CSS3&JavaScript 仿视频直播中的点赞效果

接上一篇绘制桃心型的文章 https://blog.csdn.net/chy555chy/article/details/85275704 ,这次来实现视频直播中赞出现上浮爱心的效果
在这里插入图片描述
transition 动画我在前面的 “仿京东加入购物车” 效果中也有做过类似的,很好用。

<!DOCTYPE html>
<html>
<meta charset="utf8">
<head>
<style>
#loveContainer {
	position: relative;
	width: 100px;
	height: 200px;
	border: 1px solid black;
}
.float-love {
	position: absolute;
	left: 50%;
	transform: translate(-50%);
	bottom: 0;
	transition: bottom 2s linear, left 1s ease-out, opacity 1s ease;
}
</style>
<script>
function fillLove(strokeColor, fillColor) {
	const canvas = document.createElement("canvas");
	canvas.className = "float-love"
	canvas.width = 35;
	canvas.height = 35;
	const ctx = canvas.getContext("2d");
	ctx.strokeStyle = strokeColor;
	ctx.lineWidth = 1.5;
	ctx.fillStyle = fillColor;
	ctx.save();
	ctx.translate(canvas.width/2, canvas.height/2);
	//绘制的坐标系是从左上角到右下角
	ctx.rotate(Math.PI);
	const part = 2 * Math.PI / 500;
	let t = 0;
	for(let i=0; i<500; i++,t+=part) {
		
		const x=16*Math.pow(Math.sin(t), 3);
		let y=14*Math.cos(t)-5*Math.cos(2*t)-2*Math.cos(3*t)-Math.cos(4*t)
		const tmp = Math.cos(t);
		ctx.lineTo(x,y);
	}
	ctx.fill();
	ctx.stroke();
	ctx.restore();
	return canvas;
}
window.()=>{
	const loveContainer = document.getElementById("loveContainer");
	const firstLove = fillLove("#CCC", "red");
	loveContainer.appendChild(firstLove);
	const distance = 25;
	firstLove.onclick = function(){
		const love = fillLove("#CCC", "hsl("+parseInt(Math.random()*360)+", 70%, 70%)");
		loveContainer.insertBefore(love, firstLove);
		let isFirst = true;
		love.ontransitionend=(event)=>{
			switch(event.propertyName) {
				case "bottom":
					loveContainer.removeChild(love);
				break;
				case "left":
					love.style.left = firstLove.offsetLeft+"px";
					love.style.opacity = 0;
				break;
			}
		}
		/*
		由于谷歌浏览器并不支持transitionEnd事件,必须添加前缀
		有两点需要注意:
		(1)webkitTransitionEnd的大小写一定不能错
		(2)带前缀的不能用诸如 love.onwebkittransitionend、love.onWebkitTransitionEnd 这种方式绑定事件
		*/
		//love.addEventListener("webkitTransitionEnd", love.ontransitionend);
		setTimeout(()=>{
			love.style.bottom = "150px";
			const rnd = Math.random()*distance-distance/2;
			love.style.left = firstLove.offsetLeft+rnd+(rnd<0?-distance:distance)+"px";
		},100);
	}
}
</script>
</head>
<body>
<div id="loveContainer">
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值