1. 程式人生 > >兩款已久的雪花特效

兩款已久的雪花特效

animate radi 兩種 擺動 cos loop min frame RR

一、下雪特效代碼①

 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 
 18     cancelAnimationFrame = window.cancelAnimationFrame ||
 19         window.mozCancelAnimationFrame ||
 20         window.webkitCancelAnimationFrame ||
 21         window.msCancelAnimationFrame ||
 22         window.oCancelAnimationFrame;
 23     /* 開始下雪 */
 24     snowFall.prototype.start = function(){
 25         /* 創建畫布 */
 26         snowCanvas.apply(this);
 27         /* 創建雪花形狀 */
 28         createFlakes.apply(this);
 29         /* 畫雪 */
 30         drawSnow.apply(this)
 31     }
 32     /* 創建畫布 */
 33     function snowCanvas() {
 34         /* 添加Dom結點 */
 35         var snowcanvas = document.createElement("canvas");
 36         snowcanvas.id = "snowfall";
 37         snowcanvas.width = window.innerWidth;
 38         snowcanvas.height = document.body.clientHeight;
 39         snowcanvas.setAttribute("style", "position:absolute; top: 0; left: 0; z-index: 1; pointer-events: none;");
 40         document.getElementsByTagName("body")[0].appendChild(snowcanvas);
 41         this.canvas = snowcanvas;
 42         this.ctx = snowcanvas.getContext("2d");
 43         /* 窗口大小改變的處理 */
 44         window.onresize = function() {
 45             snowcanvas.width = window.innerWidth;
 46             /* snowcanvas.height = window.innerHeight */
 47         }
 48     }
 49     /* 雪運動對象 */
 50     function flakeMove(canvasWidth, canvasHeight, flakeSize, fallSpeed) {
 51         this.x = Math.floor(Math.random() * canvasWidth);   /* x坐標 */
 52         this.y = Math.floor(Math.random() * canvasHeight);  /* y坐標 */
 53         this.size = Math.random() * flakeSize + 2;          /* 形狀 */
 54         this.maxSize = flakeSize;                           /* 最大形狀 */
 55         this.speed = Math.random() * 1 + fallSpeed;         /* 墜落速度 */
 56         this.fallSpeed = fallSpeed;                         /* 墜落速度 */
 57         this.velY = this.speed;                             /* Y方向速度 */
 58         this.velX = 0;                                      /* X方向速度 */
 59         this.stepSize = Math.random() / 30;                 /* 步長 */
 60         this.step = 0                                       /* 步數 */
 61     }
 62     flakeMove.prototype.update = function() {
 63         var x = this.x,
 64             y = this.y;
 65         /* 左右擺動(余弦) */
 66         this.velX *= 0.98;
 67         if (this.velY <= this.speed) {
 68             this.velY = this.speed
 69         }
 70         this.velX += Math.cos(this.step += .05) * this.stepSize;
 71 
 72         this.y += this.velY;
 73         this.x += this.velX;
 74         /* 飛出邊界的處理 */
 75         if (this.x >= canvas.width || this.x <= 0 || this.y >= canvas.height || this.y <= 0) {
 76             this.reset(canvas.width, canvas.height)
 77         }
 78     };
 79     /* 飛出邊界-放置最頂端繼續墜落 */
 80     flakeMove.prototype.reset = function(width, height) {
 81         this.x = Math.floor(Math.random() * width);
 82         this.y = 0;
 83         this.size = Math.random() * this.maxSize + 2;
 84         this.speed = Math.random() * 1 + this.fallSpeed;
 85         this.velY = this.speed;
 86         this.velX = 0;
 87     };
 88     // 渲染雪花-隨機形狀(此處可修改雪花顏色!!!)
 89     flakeMove.prototype.render = function(ctx) {
 90         var snowFlake = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.size);
 91         snowFlake.addColorStop(0, "rgba(255, 255, 255, 0.9)");  /* 此處是雪花顏色,默認是白色 */
 92         snowFlake.addColorStop(.5, "rgba(255, 255, 255, 0.5)"); /* 若要改為其他顏色,請自行查 */
 93         snowFlake.addColorStop(1, "rgba(255, 255, 255, 0)");    /* 找16進制的RGB 顏色代碼。 */
 94         ctx.save();
 95         ctx.fillStyle = snowFlake;
 96         ctx.beginPath();
 97         ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
 98         ctx.fill();
 99         ctx.restore();
100     };
101     /* 創建雪花-定義形狀 */
102     function createFlakes() {
103         var maxFlake = this.maxFlake,
104             flakes = this.flakes = [],
105             canvas = this.canvas;
106         for (var i = 0; i < maxFlake; i++) {
107             flakes.push(new flakeMove(canvas.width, canvas.height, this.flakeSize, this.fallSpeed))
108         }
109     }
110     /* 畫雪 */
111     function drawSnow() {
112         var maxFlake = this.maxFlake,
113             flakes = this.flakes;
114         ctx = this.ctx, canvas = this.canvas, that = this;
115         /* 清空雪花 */
116         ctx.clearRect(0, 0, canvas.width, canvas.height);
117         for (var e = 0; e < maxFlake; e++) {
118             flakes[e].update();
119             flakes[e].render(ctx);
120         }
121         /*  一幀一幀的畫 */
122         this.loop = requestAnimationFrame(function() {
123             drawSnow.apply(that);
124         });
125     }
126     /* 調用及控制方法 */
127     var snow = new snowFall({maxFlake:60});
128     snow.start();
129 </script>

使用方法:

方法①、復制其中一種JS代碼,粘貼到網站</body>標簽之前即可;

方法②、去掉代碼前後的<script **>標簽,然後將代碼保存為js文件,最後在網站引用即可。

Ps:若沒效果,請確認網頁是否已載入JQurey,如果沒有請在下雪代碼之前引入JQ即可。

載入jquery

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

兩款已久的雪花特效