<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> div{ height: 5px; width: 5px; background: red; position: absolute; } </style> <script> function getPos (ev) { var scrollTop = document.documentElement.srollTop||document.body.scrollTop; var scrollLeft = document.documentElement.scrollLeft||document.body.scrollLeft; return{x:ev.clientX+scrollLeft+‘px‘,y:ev.clientY+scrollTop+‘px‘} } document.onmousemove = function (ev){ var oDiv = document.getElementsByTagName(‘div‘); var oEvent = ev||event; 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 = getPos(oEvent).x; oDiv[0].style.top = getPos(oEvent).y; }; </script> </head> <body> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </body> </html>
有点小疑问,不知道oDiv[0]为什么放在for循环前后都没有问题。求指导!!
时间: 2024-10-12 17:30:25