鼠标跟随事件

2017-10-25  本文已影响6人  Luyc_Han
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

  <script type="text/javascript">

    document.onmousemove = function (ev) {

      /// 为了兼容浏览器
      var scrollTop = document.documentElement.scrollTop || document.body.scrollTop


      var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft

      var oeven = ev || event

      var div = document.getElementById('div-style')

      div.style.left = oeven.clientX + scrollLeft + 'px'

      div.style.top = oeven.clientY + scrollTop + 'px'
    }


  </script>

</head>
<body style="height: 2000px; width: 3000px;">

<div id="div-style" style="width: 100px;height: 100px;background-color: #4eeda8; position: absolute">


</div>

</body>
</html>

上一篇 下一篇

猜你喜欢

热点阅读