跟着鼠标走的小图片

     <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>跟着鼠标走的小图片</title>
  <style>
    #ts {
      /* 想要使图片跟着鼠标移动,需要让图片脱离文档流 */
      position: absolute;
    }
  </style>
</head>
<body>
  <img src="images/tianshi.gif" id="ts" alt="">
  <script>
    var ts = document.getElementById('ts');
    document.onmousemove = function (e) {
      e = e || window.event;

      // ts.style.left = e.clientX - 10 + 'px';
      // ts.style.top = e.clientY - 10 + 'px';

      //当有滚动条的时候获取页面位置
      ts.style.left = e.pageX - 10 + 'px';
      ts.style.top = e.pageY - 10 + 'px';
    }
  </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_38233172/article/details/89761908