免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 發帖

[版型教學] 又到新年,一键添加下雪特效。

微信截图_20201212135605.png
2020-12-12 13:57



将下边代码放到其他头部即可。
  1. <script src="http://icode.258club.com/templates/colors/snow.htm"></script>
複製代碼
2

評分人數

thank you,very good

TOP

感謝熱心分享

TOP

好看
真不簡單

TOP

效果很赞,也不卡
厉害
flash之路欢迎你http://flashroad.joinbbs.net

TOP

  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>
複製代碼


这个也可以哈哈,不过不如版主的样式多,测试结果:测试雪花
欢迎来到夺宝奇兵中文站!
~~一个属于冒险家的天地~~

TOP

感谢分享,谢谢误解大佬

TOP

非常好看~!感谢分享!
此去泉台招旧部,旌旗十万斩阎罗。

TOP

感谢老大分享,已经在用了

2020-12-21_163613.png
2020-12-21 16:37

TOP

回復 1# 无界


    感謝大大分享

TOP

返回列表