<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js 事件event</title> <style> div{ width: 10px; height: 10px; background: red; color: #fff; padding: 10px; margin-right: 10px; position: absolute; /*这个是关键*/ } </style> <script> function getpos(ev){ var scrollTop=document.documentElement.scrollTop||document.body.scrollTop; var scrollLeft=document.documentElement.scrollLeft||document.body.scrollLeft; return {x:ev.clientX+scrollLeft,y:ev.clientY+scrollTop} } //效果是样式跟着鼠标的移动而移动 document.onmousemove=function(ev) { //鼠标移动 var oDiv=document.getElementsByTagName("div"); var oEvent=ev||event; var pos =getpos(oEvent) for(var i = oDiv.length-1;i>0;i--){ oDiv[i].style.left=oDiv[i-1].offsetLeft+"px"; oDiv[i].style.top=oDiv[i-1].offsetTop+"px"; } oDiv[0].style.left=pos.x+"px"; oDiv[0].style.top=pos.y+"px"; } </script> </head> <body> <div>我</div> <div>很</div> <div>快</div> <div>乐</div> <div>我</div> <div>很</div> <div>快</div> <div>乐</div> </body> </html>