上周末没写周记,感觉工作中没有特别需要记录的东西...其实,在工作中还是有很多小的收获,这周末一并“送上”...
1、随机撒花特效
教师节快到了,公司的产品提出一个需求:在IM(即时聊天)聊天界面弹出教师节的祝福“广告”,用户点击“发送祝福”按钮,聊天界面会随机撒花。这里的重点是随机撒花,下面会附上随机撒花的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8"> <meta content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" /> <script src="http://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> </head> <body> <div> </div> <script> $(document).ready( function(){ $.fn.snow( { minSize: 2, maxSize: 150, newOn: 200, flakeColor: '#FFFFFF' } ); }); </script> </body> </html> <script> (function($){ $.fn.snow = function(options){ var $flake = $('<div />').css({'position': 'absolute', 'top': '-50px'}).html('❄'), documentHeight = $(document).height(), documentWidth = $(document).width(), defaults = { minSize : 2, maxSize : 2, newOn : 50, flakeColor : "#FFFFFF" }, options = $.extend({}, defaults, options); //setInterval-setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。 //开始一个周期-开始添加雪花 var interval = setInterval( function(){ var startPositionLeft = Math.random() * documentWidth - 100, startOpacity = 0.5 + Math.random(), sizeFlake = options.minSize + Math.random() * options.maxSize, endPositionTop = documentHeight - 40, endPositionLeft = startPositionLeft - 100 + Math.random() * 200, 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); //结束周期-停止添加雪花 setTimeout(function(){ window.clearInterval(interval); },5000); }; })(jQuery); </script>