前些天總看到一些網站下起了雪,然後聯盟也發現下雪了!SAE 也下了!
之後零零落落的在一些網站上看到了一些程式碼,但是總是沒有多麼仔細的看
今天元旦嘛,去張戈那逛了逛,發現他哪裡也有分享!
感覺他那裡的也不錯,就分享下吧!還有一會去一網站挖一下雪特效,改天一併也發了!
那個特效也不錯可以滑鼠控制雪花方向!
看程式碼之前,先分享一下即時預覽的方法:很簡單,在 webkit 瀏覽器 (比如谷歌) 按下 F12,然後在 console 裡面貼上一下 JS 程式碼 (不含前後的 script 標籤),然後回車執行即可看到效果了。

我就霸氣一回,兩種程式碼都用上!!
一、下雪特效程式碼①
<script type="text/javascript">
(function($){
$.fn.snow = function(options){
var $flake = $('<div id="snowbox" />').css({'position': 'absolute','z-index':'9999', 'top': '-50px'}).html('❄'),
documentHeight = $(document).height(),
documentWidth = $(document).width(),
defaults = {
minSize : 10,
maxSize : 20,
newOn : 1000,
flakeColor : "#AFDAEF" /* 此處可以定義雪花顏色,若要白色可以改為 #FFFFFF */
},
options = $.extend({}, defaults, options);
var interval= setInterval( function(){
var startPositionLeft = Math.random() * documentWidth - 100,
startOpacity = 0.5 + Math.random(),
sizeFlake = options.minSize + Math.random() * options.maxSize,
endPositionTop = documentHeight - 200,
endPositionLeft = startPositionLeft - 500 + Math.random() * 500,
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);
};
})(jQuery);
$(function(){
$.fn.snow({
minSize: 5, /* 定義雪花最小尺寸 */
maxSize: 50,/* 定義雪花最大尺寸 */
newOn: 300 /* 定義密集程度,數字越小越密集 */
});
});
</script>
二、下雪特效程式碼②
<script type="text/javascript">
/* 控制下雪 */
function snowFall(snow) {
/* 可配置屬性 */
snow = snow || {};
this.maxFlake = snow.maxFlake || 200; /* 最多片數 */
this.flakeSize = snow.flakeSize || 10; /* 雪花形狀 */
this.fallSpeed = snow.fallSpeed || 1; /* 墜落速度 */
}
/* 相容寫法 */
requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function(callback) { setTimeout(callback, 1000 / 60); };
cancelAnimationFrame = window.cancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.msCancelAnimationFrame ||
window.oCancelAnimationFrame;
/* 開始下雪 */
snowFall.prototype.start = function(){
/* 建立畫布 */
snowCanvas.apply(this);
/* 建立雪花形狀 */
createFlakes.apply(this);
/* 畫雪 */
drawSnow.apply(this)
}
/* 建立畫布 */
function snowCanvas() {
/* 新增 Dom 結點 */
var snowcanvas = document.createElement("canvas");
snowcanvas.id = "snowfall";
snowcanvas.width = window.innerWidth;
snowcanvas.height = document.body.clientHeight;
snowcanvas.setAttribute("style", "position:absolute; top: 0; left: 0; z-index: 1; pointer-events: none;");
document.getElementsByTagName("body")[0].appendChild(snowcanvas);
this.canvas = snowcanvas;
this.ctx = snowcanvas.getContext("2d");
/* 視窗大小改變的處理 */
window.onresize = function() {
snowcanvas.width = window.innerWidth;
/* snowcanvas.height = window.innerHeight */
}
}
/* 雪運動物件 */
function flakeMove(canvasWidth, canvasHeight, flakeSize, fallSpeed) {
this.x = Math.floor(Math.random() * canvasWidth); /* x 座標 */
this.y = Math.floor(Math.random() * canvasHeight); /* y 座標 */
this.size = Math.random() * flakeSize + 2; /* 形狀 */
this.maxSize = flakeSize; /* 最大形狀 */
this.speed = Math.random() * 1 + fallSpeed; /* 墜落速度 */
this.fallSpeed = fallSpeed; /* 墜落速度 */
this.velY = this.speed; /* Y 方向速度 */
this.velX = 0; /* X 方向速度 */
this.stepSize = Math.random() / 30; /* 步長 */
this.step = 0 /* 步數 */
}
flakeMove.prototype.update = function() {
var x = this.x,
y = this.y;
/* 左右擺動 (餘弦) */
this.velX *= 0.98;
if (this.velY <= this.speed) {
this.velY = this.speed
}
this.velX += Math.cos(this.step += .05) * this.stepSize;
this.y += this.velY;
this.x += this.velX;
/* 飛出邊界的處理 */
if (this.x >= canvas.width || this.x <= 0 || this.y >= canvas.height || this.y <= 0) {
this.reset(canvas.width, canvas.height)
}
};
/* 飛出邊界-放置最頂端繼續墜落 */
flakeMove.prototype.reset = function(width, height) {
this.x = Math.floor(Math.random() * width);
this.y = 0;
this.size = Math.random() * this.maxSize + 2;
this.speed = Math.random() * 1 + this.fallSpeed;
this.velY = this.speed;
this.velX = 0;
};
// 渲染雪花-隨機形狀 (此處可修改雪花顏色!!!)
flakeMove.prototype.render = function(ctx) {
var snowFlake = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.size);
snowFlake.addColorStop(0, "rgba(255, 255, 255, 0.9)"); /* 此處是雪花顏色,預設是白色 */
snowFlake.addColorStop(.5, "rgba(255, 255, 255, 0.5)"); /* 若要改為其他顏色,請自行查 */
snowFlake.addColorStop(1, "rgba(255, 255, 255, 0)"); /* 找 16 進位制的 RGB 顏色程式碼。 */
ctx.save();
ctx.fillStyle = snowFlake;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
};
/* 建立雪花-定義形狀 */
function createFlakes() {
var maxFlake = this.maxFlake,
flakes = this.flakes = [],
canvas = this.canvas;
for (var i = 0; i < maxFlake; i++) {
flakes.push(new flakeMove(canvas.width, canvas.height, this.flakeSize, this.fallSpeed))
}
}
/* 畫雪 */
function drawSnow() {
var maxFlake = this.maxFlake,
flakes = this.flakes;
ctx = this.ctx, canvas = this.canvas, that = this;
/* 清空雪花 */
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var e = 0; e < maxFlake; e++) {
flakes[e].update();
flakes[e].render(ctx);
}
/* 一幀一幀的畫 */
this.loop = requestAnimationFrame(function() {
drawSnow.apply(that);
});
}
/* 呼叫及控制方法 */
var snow = new snowFall({maxFlake:60});
snow.start();
</script>
使用方法:
方法①、複製其中一種 JS 程式碼,貼上到網站</body> 標籤之前即可;
方法②、去掉程式碼前後的<script **> 標籤,然後將程式碼儲存為 js 檔案,最後在網站引用即可。
Ps:若沒效果,請確認網頁是否已載入 JQurey,如果沒有請在下雪程式碼之前引入 JQ 即可。
載入 jquery
還是老樣子,載入的百度提供的庫
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.js"></script> <script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>