本文實例講述了JS實現的簡單鼠標跟隨DiV層效果。分享給大家供大家參考,具體如下:
這段代碼呈現一串跟隨鼠標的Div效果,並有拖影特效,隨著鼠標快速的晃動,Div層效果會不斷的增加,後面的Div會自動消失,不過還有些Bug,期待與大家完善這個JS特效。
運行效果截圖如下:
在線演示地址如下:
http://demo.jb51.net/js/2015/js-simple-mouse-over-div-codes/
具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style> div {width:10px; height:10px; background:red; position:absolute;} </style> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>跟隨鼠標的Div效果</title> <script> window.onload=function () { var aDiv=document.getElementsByTagName('div'); var i=0; setInterval(function(){ for(i=aDiv.length-1;i>0;i--) { aDiv[i].style.left=aDiv[i-1].style.left; aDiv[i].style.top=aDiv[i-1].style.top; } },5); document.onmousemove=function (ev) { var oEvent=ev||event; aDiv[0].style.left=oEvent.clientX+'px'; aDiv[0].style.top=oEvent.clientY+'px'; }; }; </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> <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> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </body> </html>
希望本文所述對大家JavaScript程序設計有所幫助。