Connect_logo_6.png 退出
前端视频教程 小程序 众筹源码 python 商城源码 商城模板 响应式模板 中文模板 手机模板 企业源码

jQuery+css3橙汁汽泡动画特效

作者/代码整理:  (转载请附加本文地址,带有“懒人原生”字样的谢绝转载) 发布日期:2018-04-04
一款非常好看的jQuery+css3橙汁汽泡动画特效,类似橙汁里面的气泡浮动动画效果,把背景换成蓝色,就成了大海里的汽泡了。


js代码

<script>
const floatOn = options => {
	let el = options.el,
		x = options.x,
		xIsPos = options.xIsPos || Math.floor(Math.random()),
		updateX = options.updateX || Math.floor(Math.random()),
		curTop = parseInt(el.style.top),
		curLeft = parseInt(el.style.left);

	if (curTop > -50) {
		el.style.top = `${--curTop}px`;
	} else {
		el.style.top = `${innerHeight + 50}px`;
	}

	if (updateX) {
		if (xIsPos) {
			if (curLeft > x + 10) {
				xIsPos = false;
			} else {
				el.style.left = `${curLeft + 1}px`;
			}
		} else {
			if (curLeft < x - 10) {
				xIsPos = true;
			} else {
				el.style.left = `${--curLeft}px`;
			}
		}
	}

	updateX = updateX ? false : true;

	requestAnimationFrame(
		floatOn.bind(null, { el: el, x: x, xIsPos: xIsPos, updateX: updateX })
	);
};

class Bubble {
	constructor(target, i) {
		this.bubble = document.createElement("div");
		this.bubble.classList.add("bubble");
		this.x = Math.floor(Math.random() * innerWidth);
		this.y = Math.floor(Math.random() * innerHeight);
		this.scale = Math.random();
		this.pos = Math.round(Math.random());

		this.bubble.style.top = `${this.y}px`;
		this.bubble.style.left = `${this.x}px`;
		this.bubble.style.transform = `translateZ(${
			this.pos ? "" : "-"
		}${this.scale.toFixed(2) * 1000}px)`;

		setTimeout(() => {
			target.appendChild(this.bubble);
		}, i * 50);
		setTimeout(floatOn.bind(null, { el: this.bubble, x: this.x }), i * 50);
	}
}

for (let i = 0; i < 100; i++) {
	new Bubble(document.querySelector(".soda"), i);
}
</script>