DOM:让一个元素跟随鼠标移动而移动

  • DOM:让一个元素跟随鼠标移动而移动已关闭评论
  • 108 次浏览
  • A+
所属分类:Web前端
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #a {
            width: 200px;
            height: 30px;
            border: 1px solid #000;
            text-align: center;
            list-style: none;
            background-color: pink;
        }

        #b {
            width: 200px;
            height: 230px;
            border: 1px solid #000;
            text-align: center;
            list-style: none;
            background-color: pink;
        }

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

<body>
    <ul id="ula">
        <li id="a">目录</li>
        <li id="b">内容</li>
    </ul>
    <!-- <div id="ula"></div> -->
    <script>
        var ula = document.querySelector("#ula")

        document.addEventListener("mousemove", function (e) {
            var x = e.pageX
            var y = e.pageY
            ula.style.left = x - 130 + "px";
            ula.style.top = y - 130 + "px";

        })
    </script>
</body>

</html>