當前位置:軟件學堂 > 資訊首頁 > 網(wǎng)絡編程 > 編程其他 > JS實現(xiàn)LOGO像雪花一樣落下的特效

JS實現(xiàn)LOGO像雪花一樣落下的特效

2012/11/8 11:54:42作者:佚名來源:網(wǎng)絡

移動端

【實例名稱】

JS實現(xiàn)LOGO像雪花一樣落下的特效

【實例描述】

在頁面中可以經(jīng)??吹揭恍┫裱┗ㄒ粯勇湎碌奈谋尽V告、圖片等。本例將以具體實例演示如何設計像雪花一樣落下的LOGO。

【實例代碼】

<HTML><HEAD><TITLE>LOGO從上落下-學無憂(m.wangbatian.cn)</title> <SCRIPT language=JavaScript> //窗口改變時重新加載頁面  -Netscape的瀏覽器 function MM_reloadPage(init) {    if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {     document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}   else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload(); } MM_reloadPage(true); //窗口改變時重新加載頁面  -IE瀏覽器 function MM_preloadImages() {   var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();     var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)     if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}   sjLayer.style.visibility='hidden';   eslayer.style.visibility='hidden'; }

function MM_swapImgRestore() {   var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc; }

function MM_findObj(n, d) {   var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {     d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}   if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];   for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x; }

function MM_showHideLayers() {   var i,p,v,obj,args=MM_showHideLayers.arguments;   for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];     if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }     obj.visibility=v; } } </SCRIPT> </HEAD> <BODY  > <SCRIPT language=JavaScript> //判斷瀏覽器 var ns4up = (document.layers) ? 1 : 0;  var ie4up = (document.all) ? 1 : 0; var no = 3;                             // 顯示的圖像數(shù)量 var speed = 20;                         // 圖像移動的速度 var snowflake ="LOGO1.gif";      //圖像地址 var filen = http://www.baidu.com  // 單擊圖像時的鏈接 var dx, xp, yp;                        var am, stx, sty;                      var i, doc_width = 400, doc_height = 600; if (ns4up) { doc_width = self.innerWidth; doc_height = self.innerHeight; } else if (ie4up) { doc_width = document.body.clientWidth; doc_height = document.body.clientHeight; } dx = new Array(); xp = new Array(); yp = new Array(); am = new Array(); stx = new Array(); sty = new Array(); for (i = 0; i < no; ++ i) {  dx[i] = 0;                             // set coordinate variables xp[i] = Math.random()*(doc_width-50);  // set position variables yp[i] = Math.random()*doc_height; am[i] = Math.random()*20;              // set amplitude variables stx[i] = 0.02 + Math.random()/10;      // set step variables sty[i] = 0.7 + Math.random();          // set step variables if (ns4up) {                           // set layers if (i == 0) { document.write("<html><title>-學無憂(m.wangbatian.cn) </title><body>"); document.write("<layer name=\"dot"+ i +"\" left=\"15\" "); document.write("top=\"15\" visibility=\"show\"><A href=\"" + filen + "\" target=\"_blank\"><img src=\""); document.write(snowflake + "\" border=\"0\"></layer>"); document.write("</body></html>"); } else { document.write("<html><title>-學無憂(m.wangbatian.cn)</title><body>"); document.write("<layer name=\"dot"+ i +"\" left=\"15\" "); document.write("top=\"15\" visibility=\"show\"><A href=\"" + filen + "\" target=\"_blank\"><img src=\""); document.write(snowflake + "\" border=\"0\"></layer>"); document.write("</body></html>"); } } else if (ie4up) { if (i == 0) { document.write("<html><title>-學無憂(m.wangbatian.cn) </title><body>"); document.write("<div id=\"dot"+ i +"\" style=\"POSITION: "); document.write("absolute; Z-INDEX: "+ i +"; VISIBILITY: "); document.write("visible; TOP: 15px; LEFT: 15px;\"> <a href=\""+filen+"\" target=\"_blank\"><img src=\""); document.write(snowflake + "\" border=\"0\"></div>"); document.write("</body></html>"); } else { document.write("<html><title>-學無憂(m.wangbatian.cn)</title><body>"); document.write("<div id=\"dot"+ i +"\" style=\"POSITION: "); document.write("absolute; Z-INDEX: "+ i +"; VISIBILITY: "); document.write("visible; TOP: 15px; LEFT: 15px;\"><a href=\""+filen+"\" target=\"_blank\"><img src=\""); document.write(snowflake + "\" border=\"0\"></div>"); document.write("</body></html>"); } } } //實現(xiàn)Netscape瀏覽器中動畫效果的方法 function snowNS() {     for (i = 0; i < no; ++ i)     {         yp[i] += sty[i];         if (yp[i] > doc_height-50) {         xp[i] = Math.random()*(doc_width-am[i]-30);         yp[i] = 0;         stx[i] = 0.02 + Math.random()/10;         sty[i] = 0.7 + Math.random();         doc_width = self.innerWidth;         doc_height = self.innerHeight;         } dx[i] += stx[i]; //指定圖像的顯示位置:x和y坐標 document.layers["dot"+i].top = yp[i]; document.layers["dot"+i].left = xp[i] + am[i]*Math.sin(dx[i]);     }     setTimeout("snowNS()", speed); } //實現(xiàn)IE瀏覽器中動畫效果的方法 function snowIE() {      //判斷圖像的數(shù)量     for (i = 0; i < no; ++ i) {          yp[i] += sty[i];         if (yp[i] > doc_height-50) {         xp[i] = Math.random()*(doc_width-am[i]-30);         yp[i] = 0;         stx[i] = 0.02 + Math.random()/10;         sty[i] = 0.7 + Math.random();         //重新獲取文檔的高度和寬度         doc_width = document.body.clientWidth;         doc_height = document.body.clientHeight;         } dx[i] += stx[i]; //指定圖像的顯示位置:x和y坐標 document.all["dot"+i].style.pixelTop = yp[i]; document.all["dot"+i].style.pixelLeft = xp[i] + am[i]*Math.sin(dx[i]);     }     setTimeout("snowIE()", speed);  //根據(jù)速度循環(huán)執(zhí)行動畫方法 } //判斷瀏覽器類型,并調(diào)用不同的動畫方法 if (ns4up) {  snowNS(); } else if (ie4up) {  snowIE(); } </SCRIPT> </TABLE> </BODY></HTML>

【運行效果】

 LOGO像雪花一樣落下運行效果

【難點剖析】

本例的重點是如何動態(tài)添加div并在div中加載LOGO圖片,同時還要設置這些LOGO圖片的位置、顯示速度等。設置落下的LOGO數(shù)量可修改“no”變量。

【源碼下載】

為了JS代碼的準確性,請點擊:LOGO像雪花一樣落下 進行本實例源碼下載 

標簽: JS  特效