js雪花飞舞效果,从原生版到面向对象版

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		body{
			background: #000;
			overflow: hidden;
		}
	</style>
</head>
<body>
<script>
// 纯原生js版
function flySnow(){
	var snow = document.createElement("div");
	snow.style.cssText = 'position:absolute;color:#fff;';
	var vw = document.documentElement.clientWidth - 20,
		vh = document.documentElement.clientHeight - 5;
	setInterval(function(){
		var cloneSnow = snow.cloneNode();
		var arr = ['❄','❉','❅','❆','✻','✼','❇','❈','❊','✥','✺'],
			style = arr[Math.floor(Math.random() * 11)];
			cloneSnow.innerHTML = style;
		var startLeft = vw * Math.random(),
			endLeft = vw * Math.random();
		var startOpa = 0.8 + 0.2 * Math.random(),
			endOpa = 0.2 + 0.2 * Math.random();
		var startSize = 6 + 25 * Math.random(),
			endSize = 5 + 18 * Math.random();
		var duration = 5000 + 6000 * Math.random();
		cloneSnow.style.cssText += 'left:'+startLeft+'px;'
								 + 'top:'+-22+'px;'
								 + 'opacity:'+startOpa+";"
								 + 'font-size:'+startSize+'px;'
								 + 'transition:'+duration+'ms;';
		document.body.appendChild(cloneSnow);
		setTimeout(function(){
			cloneSnow.style.cssText += 'left:'+endLeft+'px;'
									 + 'top:'+(vh - endSize)+'px;'
									 + 'opacity:'+endOpa+';'
									 + 'font-size:'+endSize+'px;'
									 + 'transform:'+'rotateZ(720deg);';
			setTimeout(function(){
				cloneSnow.remove();
			},duration)
		},0)
	},15)
}
flySnow();
</script>


<script src="jquery-1.12.4.js"></script>
<script>
//面向对象版
let flySnow = (function(){
	function Fn(){
		this.snow = function(d){
			let vw = $(document).innerWidth(),
				vh = $(window).height();
			d = d || 5000 + 6000 * Math.random();
			d = d === "fast" ? 4000 + 4000 * Math.random() : d === "slow" ? 7000 + 8000 * Math.random() : d;
			this.element = document.createElement("div");
			this.startLeft = vw * Math.random();
			this.endLeft = vw * Math.random();
			this.endTop = vh;
			this.startOpa = 0.8 + 0.2 * Math.random();
			this.endOpa = 0.2 + 0.2 * Math.random();
			this.startSize = 6 + 25 * Math.random();
			this.endSize = 5 + 18 * Math.random();
			this.duration = d ;
			this.style = (function(){
					let arr = ['❄','❉','❅','❆','✻','✼','❇','❈','❊','✥','✺'];
					return arr[Math.floor(Math.random() * arr.length)];
			})();
		}
		this.fly = function(s, d){
			setInterval(()=>{
				let snow = new this.snow(d);
				snow.element.innerText = snow.style;
				snow.element.style.cssText = `position: absolute;
											  color: #fff;
											  left: ${snow.startLeft}px;
											  top: -22px;
											  opacity: ${snow.startOpa};
											  font-size: ${snow.startSize}px;
											  transition: ${snow.duration}ms;`;
				$("body").append(snow.element);
				setTimeout(()=>{
					snow.element.style.cssText += `left: ${snow.endLeft}px;
												  top: ${snow.endTop - 20}px;
												  opacity: ${snow.endOpa};
												  font-size: ${snow.endSize}px;
												  transform: rotate(720deg);`;
					setTimeout(()=>{
						snow.element.remove()
						snow = null;
					}, snow.duration)
				}, 0)
			}, s)
		}
	}
	return new Fn();
}());
flySnow.fly(30, "slow");
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值