本篇文章给大家介绍一下css+js实现一个“爱之满满”点赞按钮的方法,希望对大家有所帮助!

前段时间在看一档说唱节目,被里面的一个说唱歌手JBcob的爱之满满这句词给洗脑了。
于是这次给大家带来一个爱之满满的点赞按钮,让大家在点赞的同时还能感受到被爱包裹的感觉。

立即学习“前端免费学习笔记(深入)”;
svg,可以用图片,我这里就用伪元素的方式做一个爱心。(学习视频分享:css视频教程)<!-- fullLove.html -->
<div class="likeBtn" id="likeBtn">
<span class="heart" id="heart"></span>
</div>/* fullLove.css */
.heart{
background-color: #8a93a0;
height: 13px;
width: 13px;
transform: rotate(-45deg) scale(1);
display: inline-block;
}
.heart::before {
content: '';
position: absolute;
top: -50%;
left: 0;
background-color: inherit;
border-radius: 50%;
height: 13px;
width: 13px;
}
.heart::after {
content: '';
position: absolute;
top: 0;
right: -50%;
background-color: inherit;
border-radius: 50%;
height: 13px;
width: 13px;
}
// love.js
const likeBtn = document.getElementById('likeBtn');
const heart=document.getElementById('heart')
likeBtn.addEventListener('mousemove',() => {
heart.classList.add('heratPop')
})
likeBtn.addEventListener('mouseout',() => {
heart.classList.remove('heratPop')
})/* fullLove.css */
.heratPop{
animation: pulse 1s linear infinite;
}
@keyframes pulse {
0% {
transform: rotate(-45deg) scale(1);
}
10% {
transform: rotate(-45deg) scale(1.1);
}
20% {
transform: rotate(-45deg) scale(0.9);
}
30% {
transform: rotate(-45deg) scale(1.2);
}
40% {
transform: rotate(-45deg) scale(0.9);
}
50% {
transform: rotate(-45deg) scale(1.1);
}
60% {
transform: rotate(-45deg) scale(0.9);
}
70% {
transform: rotate(-45deg) scale(1);
}
}
document.createElement,移除元素可以使用DOM 的remove()
// love.js
function addHearts(content) {
for(let i=0; i<10; i++) {
setTimeout(() => {
const fullHeart = document.createElement('div');
fullHeart.classList.add('hearts');
fullHeart.innerHTML = '<span class="heart"></span>';
fullHeart.style.left = Math.random() * 100 + '%';
fullHeart.style.top = Math.random() * 100 + '%';
fullHeart.style.transform = `translate(-50%, -50%) scale(${Math.random()+0.3}) `
fullHeart.style.animationDuration = Math.random() * 2 + 3 + 's';
fullHeart.firstChild.style.backgroundColor='#ed3056'
content.appendChild(fullHeart);
setTimeout(() => {
fullHeart.remove();
}, 3000);
}, i * 100)
}
}/* fullLove.css */
.hearts {
position: absolute;
color: #E7273F;
font-size: 15px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
animation: fly 3s linear forwards;
}
@keyframes fly {
to {
transform: translate(-50%, -50px) scale(0);
}
}
爱之满满点赞效果,希望可以帮助到有需要的同学。更多编程相关知识,请访问:编程学习!!
以上就是CSS+JS实现爱心点赞按钮(代码示例)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号