-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathripple-button.html
86 lines (76 loc) · 2.06 KB
/
ripple-button.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.ripple-button {
padding: 0;
position: relative;
overflow: hidden;
margin: auto;
cursor: pointer;
user-select: none;
}
.ripple {
background-color: rgba(0,0,0,0.15);
position: absolute;
border-radius: 100%;
pointer-events: none;
transform: scale(0);
opacity: 1;
}
.ripple-active {
opacity: 0;
transform: scale(2);
transition: opacity 1.2s ease-out, transform 0.6s ease-out;
}
.normal {
height: 40px;
width: 100px;
background-color: steelblue;
color: white;
border-radius: 10px;
text-align: center;
line-height: 40px;
}
</style>
</head>
<body>
<div class="ripple-button normal">
click
</div>
<script type="text/javascript">
let btns = document.getElementsByClassName('ripple-button');
function createRipple() {
let ripple = document.createElement('span');
let { classList } = ripple;
classList.add('ripple');
return {
ripple,
classList
}
}
for(let btn of btns) {
btn.addEventListener('click',function(e) {
let { width, height, top, left } = btn.getBoundingClientRect();
// let { layerY, layerX } = e
// console.log(layerX,layerY)
let { ripple, classList } = createRipple()
btn.appendChild(ripple)
let r = Math.max(width, height);
ripple.style.height = ripple.style.width = r + 'px';
ripple.style.top = e.layerY - r/2 + 'px';
ripple.style.left = e.layerX - r/2 + 'px';
classList.add('ripple-active');
setTimeout(function() {
btn.removeChild(ripple);
},1200)
})
}
// 总结
// 使用 pointer-events: none 来避免 ripple互相影响导致 layerX 和 layerY 坐标不正确
// 使用到 layerX 和 layerY 时, 再去 evt 对象读取, 达到 js 阻塞 UI 来实现 ripple 过渡效果
// 提前定义好 layerX 和 layerY 之后, 后续调用 e.layerX 或 e.layerY 都是从定义好的地方取,不是重新去 evt 对象读取
</script>
</body>
</html>