當(dāng)前位置:軟件學(xué)堂 > 資訊首頁(yè) > 網(wǎng)絡(luò)編程 > 編程其他 > 跟隨鼠標(biāo)的星星JS代碼

跟隨鼠標(biāo)的星星JS代碼

2012/10/23 18:22:24作者:佚名來(lái)源:網(wǎng)絡(luò)

移動(dòng)端

【實(shí)例名稱】

跟隨鼠標(biāo)的星星

【實(shí)例描述】

為了增加鼠標(biāo)的顯示效果,本例在鼠標(biāo)周圍布置一彩色的星星,這星星會(huì)跟隨鼠標(biāo)移動(dòng),而且實(shí)現(xiàn)閃爍效果。

【實(shí)例代碼】

 <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>標(biāo)題頁(yè)</title> </head> <body> 看看鼠標(biāo)周圍的效果,是不是五顏六色的星星。 <script language="javascript"> function Div_Layer(divleft,divtop,divfnx, divfny,mydiv,divbilder,divloop,divto,divcnt,divstep) {  if ((document.layers)||(document.all)){   with (Math) {yynextx= eval(divfnx)}                      //設(shè)置為數(shù)值型數(shù)據(jù)   with (Math) {yynexty= eval(divfny)}   divcnt=(divloop && divcnt>=divstep*divbilder)?0:divcnt+divstep;   if (document.layers){     eval(mydiv+".top="+(yynexty+divtop))                 //netscape下設(shè)置橫坐標(biāo)和縱坐標(biāo)     eval(mydiv+".left="+(yynextx+divleft))   }   if (document.all){     eval("mydiv=mydiv.replace(/.layers/gi, '.all')");     eval(mydiv+".style.pixelTop="+(yynexty+divtop));     //設(shè)置div的橫坐標(biāo)     eval(mydiv+".style.pixelLeft="+(yynextx+divleft));   //設(shè)置div的縱坐標(biāo)   }   argStr='Div_Layer('+divleft+','+divtop+',"'+divfnx+'","' +divfny+'","'+mydiv+'",'+divbilder+','+divloop+','+divto+

','+divcnt+','+divstep+')';   if (divcnt<=divstep*divbilder){   eval(mydiv+".divto=setTimeout(argStr,divto)");           //設(shè)置定時(shí)器-實(shí)現(xiàn)星星的閃爍效果   }  } } function YY_Mousetrace(evnt)                             //設(shè)置鼠標(biāo)移動(dòng)的事件 {  if (yyns4)                                              //Netscape瀏覽器的情況   {if (evnt.pageX) {yy_ml=evnt.pageX;  yy_mt=evnt.pageY;}  }  else{    yy_ml=(event.clientX + document.body.scrollLeft);     //橫坐標(biāo)位置    yy_mt=(event.clientY + document.body.scrollTop);      //縱坐標(biāo)位置  }  if (yy_tracescript)eval(yy_tracescript)                 //轉(zhuǎn)換為數(shù)值型變量 } </script> <div id="tooltip2"    style="position:absolute;visibility:hidden;clip:rect(0 150 50 0); width:150px;background-color:lightyellow">   <layer name="nstip" width="1000px" bgcolor="lightyellow"> </layer> </div>       <div id="yyd0" style="position:absolute; left:10px; top:50px; width:3px; height:3px; z-index:1; background-color: #19636c; layer-background-color: #19636c; border: 1px none #000000; clip: rect(0 3 3 0)"></div>       <div id="yyd1" style="position:absolute; left:20px; top:50px; width:3px; height:3px; z-index:1; background-color: #708574; layer-background-color: #708574; border: 1px none #000000; clip: rect(0 3 3 0)"></div>       <div id="yyd2" style="position:absolute; left:30px; top:50px; width:3px; height:3px; z-index:1; background-color: #379bbf; layer-background-color: #379bbf; border: 1px none #000000; clip: rect(0 3 3 0)"></div>       <div id="yyd3" style="position:absolute; left:40px; top:50px; width:3px; height:3px; z-index:1; background-color: #25184c; layer-background-color: #25184c; border: 1px none #000000; clip: rect(0 3 3 0)"></div>       <div id="yyd4" style="position:absolute; left:50px; top:50px; width:3px; height:3px; z-index:1; background-color: #31bd3c; layer-background-color: #31bd3c; border: 1px none #000000; clip: rect(0 3 3 0)"></div>       <div id="yyd5" style="position:absolute; left:60px; top:50px; width:3px; height:3px; z-index:1; background-color: #c11efd; layer-background-color: #c11efd; border: 1px none #000000; clip: rect(0 3 3 0)"></div> <script language="javascript">     var yyns4=window.Event?true:false; var yy_mt = 0; var yy_ml = 0; //點(diǎn)的坐標(biāo)     document.onmousemove = YY_Mousetrace;                           //綁定鼠標(biāo)移動(dòng)的事件     yy_tracescript = '';     Div_Layer(0,0,'yy_ml+cos((15*sin(divcnt/39.83007847812662))+0) *150*(sin(10+divcnt/20)+0.2)*cos(divcnt/20)','yy_mt+sin((15*sin (divcnt/34.224861639800686))+0)*150*(sin(10+divcnt/20)+0.2)*cos (divcnt/20)','document.layers[\'yyd0\']',2000,true,80,0,1);     Div_Layer(0,0,'yy_ml+cos((15*sin(divcnt/27.66510707209673))+30) *150*(sin(10+divcnt/20)+0.2)*cos(divcnt/20)','yy_mt+sin((15*sin (divcnt/9.240632767417667))+30)*150*(sin(10+divcnt/20)+0.2)*cos (divcnt/20)','document.layers[\'yyd1\']',2000,true,80,0,1);     Div_Layer(0,0,'yy_ml+cos((15*sin(divcnt/16.45318944579641))+60) *150*(sin(10+divcnt/20)+0.2)*cos(divcnt/20)','yy_mt+sin((15*sin (divcnt/16.0564452288292))+60)*150*(sin(10+divcnt/20)+0.2)*cos (divcnt/20)','document.layers[\'yyd2\']',2000,true,80,0,1);     Div_Layer(0,0,'yy_ml+cos((15*sin(divcnt/6.95348954836835)) +90)*150*(sin(10+divcnt/20)+0.2)*cos(divcnt/20)','yy_mt+sin( (15*sin(divcnt/44.13697049887155))+90)*150*(sin(10+divcnt/20) +0.2)*cos(divcnt/20)','document.layers[\'yyd3\']',2000,true,80,0,1);     Div_Layer(0,0,'yy_ml+cos((15*sin(divcnt/33.90077294583733)) +120)*150*(sin(10+divcnt/20)+0.2)*cos(divcnt/20)','yy_mt+sin( (15*sin(divcnt/2.2378828869411587))+120)*150*(sin(10+divcnt/20) +0.2)*cos(divcnt/20)','document.layers[\'yyd4\']',2000,true,80,0,1);     Div_Layer(0,0,'yy_ml+cos((15*sin(divcnt/37.858312521039835)) +150)*150*(sin(10+divcnt/20)+0.2)*cos(divcnt/20)','yy_mt+sin( (15*sin(divcnt/18.083839795990098))+150)*150*(sin(10+divcnt/20) +0.2)*cos(divcnt/20)','document.layers[\'yyd5\']',2000,true,80,0,1);     </script> </body> </html>

【運(yùn)行效果】

運(yùn)行效果

【難點(diǎn)剖析】

本例重點(diǎn)是這星星的布局,顏色和跳動(dòng)。星星通過(guò)div實(shí)現(xiàn),顏色通過(guò)設(shè)置div的樣式“background-color”實(shí)現(xiàn),有多少顆星星,就有多少個(gè)div。為了實(shí)現(xiàn)星星五顏六色效果,每個(gè)div的顏色都不相同。最后通過(guò)一個(gè)“setTimeOut”定時(shí)器,實(shí)現(xiàn)星星的跳動(dòng)效果。

【源碼下載】

本實(shí)例JS代碼下載

 

標(biāo)簽: 鼠標(biāo)  JS代碼