首页 > 代码库 > div随着鼠标的移动而移动

div随着鼠标的移动而移动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function getXy1(event) {
            var flow = document.getElementById("flow");
            flow.style.left = event.clientX  + 5 +"px";
            flow.style.top  = event.clientY  + 5 + "px";
            flow.innerHTML="<h6>"+event.clientX+":"+event.clientY+"</h6>";
        }
    </script>
</head>
//注意这里用的方法  onm ousemove;body里边设置了一个样式

<body onm ousemove="getXy1(event)"style="height: 1000px">
<div id="flow" style="width: 80px;height: 50px;position:absolute"></div>
</body>
</html>

div随着鼠标的移动而移动