1. 程式人生 > >js滑鼠跟隨事件及位置獲取

js滑鼠跟隨事件及位置獲取

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery.js"></script>
    <style>
        img{
            width: 40px;
            height: 40px;
            border-radius:28px;
        }
    </style>
    <script>
        $(document).ready(function(){
           $("div").mousemove(function(event){//div滑鼠移動事件
               //滑鼠位置獲取position
               var e = event || window.event;
               var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
               var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
               var x = e.pageX || e.clientX + scrollX;
               var y = e.pageY || e.clientY + scrollY;
               // console.log(x+","+y);

               //元素載入位置
               var style = "position: relative;left:"+ x + "px;top : "+ y + "px";
               // console.log(style);
               $("span").attr("style",style);
               $("span").text("haha");
           });
        });

    </script>
</head>
<body>
    <div style="background-color: aquamarine;width: 100%;height: 1024px;display: block;">
        <span></span>
    </div>
</body>
</html>

有用請打個賞^^  haha~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~