本文實例為大家分享了jQuery實現(xiàn)雪花飄落效果的具體代碼,供大家參考,具體內(nèi)容如下
效果展示:
源碼展示:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>jquery實現(xiàn)雪花飄落</title> <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script> <style> * { margin:0px; padding:0px; } body { position:relative; height:1000px; width:100%; overflow:hidden; background-color: #666; } span { display:block; opacity:0.7; } </style> </head> <body> <script> $(function() { setInterval(function() { var maxW = document.body.clientWidth, maxH = document.body.clientHeight, left = Math.random() * maxW, bottom = left - (Math.random() - 0.5) * 0.2 * maxW, //保證落下的位置水平有變化,但不大 opacity = 0.7 + 0.3 * Math.random(); speed = 30; size = 20 + 10 * Math.random(), //字體20-30 color = '#fff'; // num = Math.floor(Math.random() * 10) //產(chǎn)生0-9隨機(jī)數(shù),當(dāng)然你們可以自己設(shè)置 num ='*'; var style = 'position:absolute;top:0px;font-size:' + size + 'px;color:' + color + ';left:' + left + 'px;opacity:' + opacity; var p = '<span class = "dd" style="' + style + '">' + num + '</span>' $('body').append(p) $('span').animate({ top: maxH, left: bottom }, 3000, function() { $(this).remove() //這一步很關(guān)鍵,要把落下的去掉,不然會越積越多 }); }, 20) //20ms產(chǎn)生一個 }) </script> <pre style="color:red"> 感: 最近貢獻(xiàn)一下我在教學(xué)中的小案例 希望能給你一些幫助 ,希望大家繼續(xù)關(guān)注我的博客 --王 </pre> </body> </html>
相關(guān)學(xué)習(xí)推薦:javascript視頻教程