设为首页收藏本站 国外访客:

 找回密码
 立即注册
搜索
查看: 97|回复: 0

分享两种圣诞节网页雪花飘落特效JS代码

[复制链接]
灌水成绩
16215
62
2037676
主题
帖子
积分

等级头衔

ID : 1

管理员

积分成就 威望 : 999899
贡献 : 9999
下载币 : 11602
在线时间 : 1353 小时
注册时间 : 2013-9-5
最后登录 : 2026-2-27

发表于 2 小时前 | 显示全部楼层 |阅读模式
免责


  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:500});
  126.     snow.start();
  127. </script>
复制代码

  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>
复制代码


免责
帖子地址打造全网最多免费游戏网站,部分资源失效,私聊站长补
今日来客 列表模式
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|蔡州手游 |网站地图 wapmz.com

GMT+8, 2026-2-27 13:51