软件学堂

网游分类软件分类

跟随鼠标的星星JS代码

2012-10-23 来源:网络

【实例名称】

跟随鼠标的星星

【实例描述】

为了增加鼠标的显示效果,本例在鼠标周围布置一彩色的星星,这星星会跟随鼠标移动,而且实现闪烁效果。

【实例代码】

 <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>标题页</title> </head> <body> 看看鼠标周围的效果,是不是五颜六色的星星。 <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)}                      //设置为数值型数据   with (Math) {yynexty= eval(divfny)}   divcnt=(divloop && divcnt>=divstep*divbilder)?0:divcnt+divstep;   if (document.layers){     eval(mydiv+".top="+(yynexty+divtop))                 //netscape下设置横坐标和纵坐标     eval(mydiv+".left="+(yynextx+divleft))   }   if (document.all){     eval("mydiv=mydiv.replace(/.layers/gi, '.all')");     eval(mydiv+".style.pixelTop="+(yynexty+divtop));     //设置div的横坐标     eval(mydiv+".style.pixelLeft="+(yynextx+divleft));   //设置div的纵坐标   }   argStr='Div_Layer('+divleft+','+divtop+',"'+divfnx+'","' +divfny+'","'+mydiv+'",'+divbilder+','+divloop+','+divto+

','+divcnt+','+divstep+')';   if (divcnt<=divstep*divbilder){   eval(mydiv+".divto=setTimeout(argStr,divto)");           //设置定时器-实现星星的闪烁效果   }  } } function YY_Mousetrace(evnt)                             //设置鼠标移动的事件 {  if (yyns4)                                              //Netscape浏览器的情况   {if (evnt.pageX) {yy_ml=evnt.pageX;  yy_mt=evnt.pageY;}  }  else{    yy_ml=(event.clientX + document.body.scrollLeft);     //横坐标位置    yy_mt=(event.clientY + document.body.scrollTop);      //纵坐标位置  }  if (yy_tracescript)eval(yy_tracescript)                 //转换为数值型变量 } </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; //点的坐标     document.onmousemove = YY_Mousetrace;                           //绑定鼠标移动的事件     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>

【运行效果】

运行效果

【难点剖析】

本例重点是这星星的布局,颜色和跳动。星星通过div实现,颜色通过设置div的样式“background-color”实现,有多少颗星星,就有多少个div。为了实现星星五颜六色效果,每个div的颜色都不相同。最后通过一个“setTimeOut”定时器,实现星星的跳动效果。

【源码下载】

本实例JS代码下载

 

上一篇:跟随鼠标的魔法文字JS代码
下一篇:跟随鼠标的旋转背景JS代码

相关文章