样式根据鼠标的移动而移动

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>js 鼠标移动</title> <style> #div1{ width: 50px; height: 50px; background: red; position: absolute; /*这个是关键*/ } </style> <script> //效果是样式跟着鼠标的移动而移动 document.onmousemove=function(ev) { //鼠标移动 var oEvent=ev||event; var oDiv=document.getElementById("div1"); oDiv.style.left=oEvent.clientX+"px"; oDiv.style.top=oEvent.clientY+"px"; } </script></head><body> <div id="div1"></div></body></html>

 如果高度,宽度很大,解决方法:https://www.cnblogs.com/quitpoison/p/9900714.html

相关文章