效果
实现
思路
- 一个按钮,点击实现涟漪效果;
- 按钮有底色,他的涟漪效果需要一个在它上面的蒙版实现(定位);
- 蒙版设置动画效果,这个动画效果触发的条件是点击;
- 点击效果只有很短的时间,所以,点击之后触发动画,一定时间后关闭动画,换个思路,我们无需控制动画的触发和暂停,但是可以控制蒙版元素的创建和移除,即点击时创建,一定时间后移除;
代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style>
.box {
margin: 100px;
}
.button {
position: relative;
color: #333;
padding: 14px 40px;
background: linear-gradient(90deg, #FFF7FB, #F0F0F0);
border-radius: 45px;
margin: 0 15px;
font-size: 24px;
font-weight: 400;
text-decoration: none;
overflow: hidden;
box-shadow: 1px 1px 3px #FFECEC;
}
.button .overlay {
position: absolute;
height: 400px;
width: 400px;
background-color: #FFD2D2;
top: 0;
left: 0;
transform: translate(-50%, -50%);
border-radius: 50%;
opacity: .5;
animation: blink .5s linear infinite;
}
@keyframes blink {
0% {
opacity: .5;
height: 0px;
width: 0px;
}
100% {
opacity: 0;
height: 400px;
width: 400px;
}
}
</style>
</head>
<body>
<div class="box">
<button id="btn" class="button" onclick="addBlink(event)">
click blink
</button>
</div>
</body>
</html>
<script type="text/javascript">
const addBlink = function (e) {
console.log(e);
let overlay = document.createElement('span')
const x = e.clientX - e.target.offsetLeft
const y = e.clientY - e.target.offsetTop;
overlay.className = 'overlay'
overlay.style.left = x + 'px'
overlay.style.top = y + 'px'
document.querySelector('.button').appendChild(overlay)
setTimeout(() => {
overlay.remove()
}, 500)
}
</script>
标签:原生,动画,效果,overlay,点击,涟漪,按钮,400px
From: https://www.cnblogs.com/rain111/p/17460773.html