<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3按钮爆炸特效</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #f0f0f0;
font-family: Arial, sans-serif;
overflow: hidden;
}
.container {
text-align: center;
position: relative;
}
.title {
font-size: 2.5em;
color: #333;
margin-bottom: 50px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
}
.explode-btn {
position: relative;
padding: 15px 40px;
font-size: 1.2em;
background: linear-gradient(45deg, #FF416C, #FF4B2B);
color: white;
border: none;
border-radius: 50px;
cursor: pointer;
outline: none;
box-shadow: 0 5px 15px rgba(255, 75, 43, 0.4);
transition: all 0.3s;
overflow: hidden;
z-index: 1;
}
.explode-btn:hover {
transform: translateY(-3px);
box-shadow: 0 8px 20px rgba(255, 75, 43, 0.6);
}
.explode-btn:active {
transform: translateY(1px);
}
.explode-btn.clicked {
animation: btnShrink 0.5s forwards;
}
.particles {
position: absolute;
width: 10px;
height: 10px;
border-radius: 50%;
pointer-events: none;
z-index: 2;
}
.link {
position: absolute;
bottom: 30px;
left: 0;
width: 100%;
text-align: center;
}
.link a {
color: #FF416C;
text-decoration: none;
font-weight: bold;
padding: 8px 15px;
border-radius: 20px;
transition: all 0.3s;
}
.link a:hover {
background: rgba(255, 65, 108, 0.1);
}
@keyframes btnShrink {
0% {
transform: scale(1);
opacity: 1;
}
100% {
transform: scale(0);
opacity: 0;
}
}
@keyframes particleFly {
0% {
transform: translate(0, 0) scale(1);
opacity: 1;
}
100% {
transform: translate(var(--tx), var(--ty)) scale(0);
opacity: 0;
}
}
</style>
</head>
<body>
<div class="container">
<h1 class="title">点击按钮看爆炸效果</h1>
<button class="explode-btn" id="explodeBtn">点击爆炸</button>
<div class="link">
</div>
</div>
<script>
document.getElementById('explodeBtn').addEventListener('click', function(e) {
const btn = e.target;
btn.classList.add('clicked');
// 创建爆炸粒子
createParticles(btn);
// 重置按钮状态
setTimeout(() => {
btn.classList.remove('clicked');
}, 500);
});
function createParticles(btn) {
const btnRect = btn.getBoundingClientRect();
const centerX = btnRect.left + btnRect.width / 2;
const centerY = btnRect.top + btnRect.height / 2;
// 创建30个粒子
for (let i = 0; i < 30; i++) {
const particle = document.createElement('div');
particle.className = 'particles';
// 随机颜色
const hue = Math.floor(Math.random() * 60) + 10; // 10-70红色到橙色
particle.style.backgroundColor = `hsl(${hue}, 100%, 50%)`;
// 随机大小
const size = Math.random() * 10 + 5;
particle.style.width = `${size}px`;
particle.style.height = `${size}px`;
// 随机位置
const angle = Math.random() * Math.PI * 2;
const distance = Math.random() * 100 + 50;
const tx = Math.cos(angle) * distance;
const ty = Math.sin(angle) * distance;
particle.style.setProperty('--tx', `${tx}px`);
particle.style.setProperty('--ty', `${ty}px`);
particle.style.left = `${centerX}px`;
particle.style.top = `${centerY}px`;
// 动画
particle.style.animation = `particleFly 0.8s ease-out forwards`;
document.body.appendChild(particle);
// 动画结束后移除粒子
setTimeout(() => {
particle.remove();
}, 800);
}
}
</script>
</body>
</html>
1082

被折叠的 条评论
为什么被折叠?



