分享两款雪花特效代码

冬天到了,或者圣诞节到了,很多网站会用到雪花特效,

今天我就分享两款觉得比较好的雪花js特效代码给大家,

当然不是小弟原创的,得感谢原创作者的无私奉献

一、下雪特效代码①

该特效的雪花是有雪花的八角形状的

 


 
 
  1. <script type= "text/javascript">
  2. ( function($){
  3. $.fn.snow = function(options){
  4. var $flake = $( '<div id="snowbox" />').css({ 'position': 'absolute', 'z-index': '9999', 'top': '-50px'}).html( '❄'),
  5. documentHeight = $( document).height(),
  6. documentWidth = $( document).width(),
  7. defaults = {
  8. minSize : 10,
  9. maxSize : 20,
  10. newOn : 1000,
  11. flakeColor : "#AFDAEF" /* 此处可以定义雪花颜色,若要白色可以改为#FFFFFF */
  12. },
  13. options = $.extend({}, defaults, options);
  14. var interval= setInterval( function(){
  15. var startPositionLeft = Math.random() * documentWidth - 100,
  16. startOpacity = 0.5 + Math.random(),
  17. sizeFlake = options.minSize + Math.random() * options.maxSize,
  18. endPositionTop = documentHeight - 200,
  19. endPositionLeft = startPositionLeft - 500 + Math.random() * 500,
  20. durationFall = documentHeight * 10 + Math.random() * 5000;
  21. $flake.clone().appendTo( 'body').css({
  22. left: startPositionLeft,
  23. opacity: startOpacity,
  24. 'font-size': sizeFlake,
  25. color: options.flakeColor
  26. }).animate({
  27. top: endPositionTop,
  28. left: endPositionLeft,
  29. opacity: 0.2
  30. },durationFall, 'linear', function(){
  31. $( this).remove()
  32. });
  33. }, options.newOn);
  34. };
  35. })(jQuery);
  36. $( function(){
  37. $.fn.snow({
  38. minSize: 5, /* 定义雪花最小尺寸 */
  39. maxSize: 50, /* 定义雪花最大尺寸 */
  40. newOn: 300 /* 定义密集程度,数字越小越密集 */
  41. });
  42. });
  43. </script>

 

 

 


二、下雪特效代码②

 

该特效的雪花是小圆点形状的

 

 


 
 
  1. <script type= "text/javascript">
  2. /* 控制下雪 */
  3. function snowFall(snow) {
  4. /* 可配置属性 */
  5. snow = snow || {};
  6. this.maxFlake = snow.maxFlake || 200; /* 最多片数 */
  7. this.flakeSize = snow.flakeSize || 10; /* 雪花形状 */
  8. this.fallSpeed = snow.fallSpeed || 1; /* 坠落速度 */
  9. }
  10. /* 兼容写法 */
  11. requestAnimationFrame = window.requestAnimationFrame ||
  12. window.mozRequestAnimationFrame ||
  13. window.webkitRequestAnimationFrame ||
  14. window.msRequestAnimationFrame ||
  15. window.oRequestAnimationFrame ||
  16. function(callback) { setTimeout(callback, 1000 / 60); };
  17. cancelAnimationFrame = window.cancelAnimationFrame ||
  18. window.mozCancelAnimationFrame ||
  19. window.webkitCancelAnimationFrame ||
  20. window.msCancelAnimationFrame ||
  21. window.oCancelAnimationFrame;
  22. /* 开始下雪 */
  23. snowFall.prototype.start = function(){
  24. /* 创建画布 */
  25. snowCanvas.apply( this);
  26. /* 创建雪花形状 */
  27. createFlakes.apply( this);
  28. /* 画雪 */
  29. drawSnow.apply( this)
  30. }
  31. /* 创建画布 */
  32. function snowCanvas() {
  33. /* 添加Dom结点 */
  34. var snowcanvas = document.createElement( "canvas");
  35. snowcanvas.id = "snowfall";
  36. snowcanvas.width = window.innerWidth;
  37. snowcanvas.height = document.body.clientHeight;
  38. snowcanvas.setAttribute( "style", "position:absolute; top: 0; left: 0; z-index: 1; pointer-events: none;");
  39. document.getElementsByTagName( "body")[ 0].appendChild(snowcanvas);
  40. this.canvas = snowcanvas;
  41. this.ctx = snowcanvas.getContext( "2d");
  42. /* 窗口大小改变的处理 */
  43. window.onresize = function() {
  44. snowcanvas.width = window.innerWidth;
  45. /* snowcanvas.height = window.innerHeight */
  46. }
  47. }
  48. /* 雪运动对象 */
  49. function flakeMove(canvasWidth, canvasHeight, flakeSize, fallSpeed) {
  50. this.x = Math.floor( Math.random() * canvasWidth); /* x坐标 */
  51. this.y = Math.floor( Math.random() * canvasHeight); /* y坐标 */
  52. this.size = Math.random() * flakeSize + 2; /* 形状 */
  53. this.maxSize = flakeSize; /* 最大形状 */
  54. this.speed = Math.random() * 1 + fallSpeed; /* 坠落速度 */
  55. this.fallSpeed = fallSpeed; /* 坠落速度 */
  56. this.velY = this.speed; /* Y方向速度 */
  57. this.velX = 0; /* X方向速度 */
  58. this.stepSize = Math.random() / 30; /* 步长 */
  59. this.step = 0 /* 步数 */
  60. }
  61. flakeMove.prototype.update = function() {
  62. var x = this.x,
  63. y = this.y;
  64. /* 左右摆动(余弦) */
  65. this.velX *= 0.98;
  66. if ( this.velY <= this.speed) {
  67. this.velY = this.speed
  68. }
  69. this.velX += Math.cos( this.step += .05) * this.stepSize;
  70. this.y += this.velY;
  71. this.x += this.velX;
  72. /* 飞出边界的处理 */
  73. if ( this.x >= canvas.width || this.x <= 0 || this.y >= canvas.height || this.y <= 0) {
  74. this.reset(canvas.width, canvas.height)
  75. }
  76. };
  77. /* 飞出边界-放置最顶端继续坠落 */
  78. flakeMove.prototype.reset = function(width, height) {
  79. this.x = Math.floor( Math.random() * width);
  80. this.y = 0;
  81. this.size = Math.random() * this.maxSize + 2;
  82. this.speed = Math.random() * 1 + this.fallSpeed;
  83. this.velY = this.speed;
  84. this.velX = 0;
  85. };
  86. // 渲染雪花-随机形状(此处可修改雪花颜色!!!)
  87. flakeMove.prototype.render = function(ctx) {
  88. var snowFlake = ctx.createRadialGradient( this.x, this.y, 0, this.x, this.y, this.size);
  89. snowFlake.addColorStop( 0, "rgba(255, 255, 255, 0.9)"); /* 此处是雪花颜色,默认是白色 */
  90. snowFlake.addColorStop( .5, "rgba(255, 255, 255, 0.5)"); /* 若要改为其他颜色,请自行查 */
  91. snowFlake.addColorStop( 1, "rgba(255, 255, 255, 0)"); /* 找16进制的RGB 颜色代码。 */
  92. ctx.save();
  93. ctx.fillStyle = snowFlake;
  94. ctx.beginPath();
  95. ctx.arc( this.x, this.y, this.size, 0, Math.PI * 2);
  96. ctx.fill();
  97. ctx.restore();
  98. };
  99. /* 创建雪花-定义形状 */
  100. function createFlakes() {
  101. var maxFlake = this.maxFlake,
  102. flakes = this.flakes = [],
  103. canvas = this.canvas;
  104. for ( var i = 0; i < maxFlake; i++) {
  105. flakes.push( new flakeMove(canvas.width, canvas.height, this.flakeSize, this.fallSpeed))
  106. }
  107. }
  108. /* 画雪 */
  109. function drawSnow() {
  110. var maxFlake = this.maxFlake,
  111. flakes = this.flakes;
  112. ctx = this.ctx, canvas = this.canvas, that = this;
  113. /* 清空雪花 */
  114. ctx.clearRect( 0, 0, canvas.width, canvas.height);
  115. for ( var e = 0; e < maxFlake; e++) {
  116. flakes[e].update();
  117. flakes[e].render(ctx);
  118. }
  119. /* 一帧一帧的画 */
  120. this.loop = requestAnimationFrame( function() {
  121. drawSnow.apply(that);
  122. });
  123. }
  124. /* 调用及控制方法 */
  125. var snow = new snowFall({ maxFlake: 60});
  126. snow.start();
  127. </script>

 

 

 

 

 

使用之前,最好引入js库,否则可能不会生效

 


 
 
  1. <script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.js"> </script>
  2. <script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"> </script>

 

 

原文地址:https://ihuan.me/2172.html

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值