鼠标跟随效果

07.鼠标跟随效果

点击查看代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./lib/jquery.js"></script>
  <style>
    html,
    body {
      margin: 0;
      padding: 0;
      overflow: hidden;
    }

    #angel {
      position: absolute;
    }
  </style>
</head>

<body>
  <img src="./angel.gif" alt="" id="angel" />

  <script>
    $(function () {
      // 1. 获取到图片
      var angel = $('#angel')
      // 步骤1. 定义节流阀
      var timer = null
      // 2. 绑定 mousemove 事件
      $(document).on('mousemove', function (e) {
        // 步骤3:判断节流阀是否为空
        if (timer) { return }
        // 3. 设置图片的位置
        // 步骤2:开启延时器
        timer = setTimeout(function () {
          $(angel).css('top', e.pageY + 'px').css('left', e.pageX + 'px')
          console.log('ok')
          timer = null
        }, 16)

      })
    })
  </script>
</body>

</html>
posted @ 2022-10-09 09:35  一梦逐一生  阅读(74)  评论(0)    收藏  举报