前些天,在園子里看到了使你的博客飄雪花的文章,就趕緊弄到自己的博客里來看看效果,別說,還真是漂亮啊。可是看了一會,就發現頁面變得特別卡。
看了下代碼后發現,原作者是在body中不停的插入多個小div雪花來向下慢慢飄,一直飄到body的底部后,將雪花移除。
但是,實際上,超出屏幕的頁面我們又看不見,就算有雪花在飄又有什么意義呢。
於是,將原來的代碼稍加修改,讓他只是從屏幕的頂部飄落到屏幕底部(不是body的底部)后,就將雪花移除,另外將雪花改為fixed定位。
將頁面刷新下,果然好多了。現把修改代碼貼出來與大家分享。
原作者鏈接 http://www.cnblogs.com/lhb25/archive/2012/12/25/jquery-snow-falling-effect.html (感謝@夢想天空(山邊小溪)提供)
(function($){ $.fn.snow=function(options){ var $flake=$('<div />') .css({ 'position':'fixed',//'absolute', 'top':'-50px', 'z-index':'1000' }) .html('❄'); var documentHeight=document.documentElement.clientHeight;//$(document).height(); var documentWidth=$(document).width(); var defaults={minSize:10,maxSize:20,newOn:500,flakeColor:"#FFFFFF"}; var options=$.extend({},defaults,options); var interval=setInterval(function(){ var startPositionLeft=Math.random()*documentWidth-100; var startOpacity=0.5+Math.random(); var sizeFlake=options.minSize+Math.random()*options.maxSize; var endPositionTop=documentHeight-40; var endPositionLeft=startPositionLeft-100+Math.random()*200; var durationFall=documentHeight*10+Math.random()*5000; $flake.clone() .appendTo('body') .css({ left:startPositionLeft, opacity:startOpacity, 'font-size':sizeFlake, color:options.flakeColor }) .animate({ top:endPositionTop, left:endPositionLeft, opacity:0.2 }, durationFall, 'linear', function(){ $(this).remove(); }); },options.newOn); }; })(jQuery); $.fn.snow({ minSize: 10, maxSize: 50, newOn: 1000, flakeColor: '#ccc'});
看到有園友問如何使用,現在說下使用方法:
在博客園后台--設置--頁腳html代碼-添加一個script標簽,把上面的js代碼放進去--保存即可。