div跟随鼠标移动代码

div跟随鼠标移动事件:

<script>
window.onload = function(){
  var oTop = document.getElementById("to_top");    //这里取到所要跟随鼠标移动的div
  document.onmousemove = function(evt){
    var oEvent = evt || window.event;    //这句是重点
    var scrollleft = document.documentElement.scrollLeft || document.body.scrollLeft;
    var scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
    oTop.style.left = oEvent.clientX + scrollleft +10 +"px";       //这里可以调鼠标与div的距离  left与top
    oTop.style.top = oEvent.clientY + scrolltop + 10 + "px";  
  }
}
</script>

 

posted @ 2017-02-17 17:33  厚爱无需我多言。  阅读(297)  评论(0编辑  收藏  举报