栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

Hexo添加雪花飘落效果

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Hexo添加雪花飘落效果

为了增加博客站点的趣味性,特意添加了雪花飘落的效果。效果预览地址:https://donlex.cn ,请在PC端打开,移动端是看不到效果的。

实现方法:在 themesnextsourcejssrc 目录下新建一个 snow.js 文件,复制粘贴一下代码。

其中样式一是六边形的雪片,样式二是小圆点的雪花,其自行调试,选择喜欢的样式。


(function($){
	$.fn.snow = function(options){
	var $flake = $('').css({'position': 'absolute','z-index':'9999', 'top': '-50px'}).html('❄'),
	documentHeight 	= $(document).height(),
	documentWidth	= $(document).width(),
	defaults = {
		minSize		: 10,
		maxSize		: 20,
		newOn		: 1000,
		flakeColor	: "#AFDAEF" 
	},
	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  
    });
});



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() {
    
    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;
 
    }
}

function flakeMove(canvasWidth, canvasHeight, flakeSize, fallSpeed) {
    this.x = Math.floor(Math.random() * canvasWidth);   
    this.y = Math.floor(Math.random() * canvasHeight);  
    this.size = Math.random() * flakeSize + 2;   
    this.maxSize = flakeSize;      
    this.speed = Math.random() * 1 + fallSpeed;  
    this.fallSpeed = fallSpeed;    
    this.velY = this.speed; 
    this.velX = 0;   
    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)");    
    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();

然后在 themesnextlayout_layout.swig 文件里内部引用即可:


');
  }

上面的代码为了不影响移动端的阅读体验,特意增加了屏幕宽度的判断,请根据需要决定是否添加,或者注释掉。

最后,请确保在你添加的代码上面已经引入了JQ,否则你还需要导入jq


效果预览地址:https://www.donlex.cn ,请在PC端打开,移动端是看不到效果的。

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号