<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>按钮点击水波纹效果</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f5f5f5;
flex-direction: column;
}
.container {
text-align: center;
max-width: 800px;
padding: 20px;
}
h1 {
margin-bottom: 40px;
color: #333;
}
.btn-ripple-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
margin-bottom: 40px;
}
.btn {
position: relative;
overflow: hidden;
padding: 12px 24px;
background: #4a89dc;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
outline: none;
transition: all 0.3s;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
min-width: 120px;
}
.btn:hover {
background: #3b7dd8;
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.btn:active {
transform: translateY(0);
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.2);
}
.btn .ripple {
position: absolute;
border-radius: 50%;
background-color: rgba(255, 255, 255, 0.7);
transform: scale(0);
animation: ripple 0.6s linear;
pointer-events: none;
}
@keyframes ripple {
to {
transform: scale(4);
opacity: 0;
}
}
/* 不同颜色的按钮 */
.btn-primary {
background: #4a89dc;
}
.btn-success {
background: #48cfad;
}
.btn-warning {
background: #ffce54;
color: #333;
}
.btn-danger {
background: #ed5565;
}
.btn-info {
background: #5d9cec;
}
.btn-dark {
background: #434a54;
}
.footer {
margin-top: 50px;
color: #666;
font-size: 14px;
}
.footer a {
color: #4a89dc;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>按钮点击水波纹效果</h1>
<div class="btn-ripple-container">
<button class="btn btn-primary">主要按钮</button>
<button class="btn btn-success">成功按钮</button>
<button class="btn btn-warning">警告按钮</button>
<button class="btn btn-danger">危险按钮</button>
<button class="btn btn-info">信息按钮</button>
<button class="btn btn-dark">深色按钮</button>
</div>
<p>点击上面的按钮查看水波纹效果</p>
<div class="footer">
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const buttons = document.querySelectorAll('.btn');
buttons.forEach(button => {
button.addEventListener('click', function(e) {
// 移除之前的水波纹元素
const ripples = this.querySelectorAll('.ripple');
ripples.forEach(ripple => {
ripple.remove();
});
// 获取点击位置
const rect = this.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// 创建水波纹元素
const ripple = document.createElement('span');
ripple.classList.add('ripple');
ripple.style.left = `${x}px`;
ripple.style.top = `${y}px`;
ripple.style.width = `${Math.max(rect.width, rect.height)}px`;
ripple.style.height = `${Math.max(rect.width, rect.height)}px`;
this.appendChild(ripple);
// 动画结束后移除水波纹元素
setTimeout(() => {
ripple.remove();
}, 600);
});
});
});
</script>
</body>
</html>
6480

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



