Skip to content

Commit cf48ed2

Browse files
jsjzhPanJiaChen
authored andcommitted
fix[Waves-Directive]: fixed v-waves does not support update (PanJiaChen#1705)
1 parent 9574643 commit cf48ed2

File tree

1 file changed

+64
-33
lines changed

1 file changed

+64
-33
lines changed

src/directive/waves/waves.js

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,73 @@
11
import './waves.css'
22

3-
export default{
4-
bind(el, binding) {
5-
el.addEventListener('click', e => {
6-
const customOpts = Object.assign({}, binding.value)
7-
const opts = Object.assign({
3+
const context = '@@wavesContext'
4+
5+
function handleClick(el, binding) {
6+
function handle(e) {
7+
const customOpts = Object.assign({}, binding.value)
8+
const opts = Object.assign(
9+
{
810
ele: el, // 波纹作用元素
911
type: 'hit', // hit 点击位置扩散 center中心点扩展
1012
color: 'rgba(0, 0, 0, 0.15)' // 波纹颜色
11-
}, customOpts)
12-
const target = opts.ele
13-
if (target) {
14-
target.style.position = 'relative'
15-
target.style.overflow = 'hidden'
16-
const rect = target.getBoundingClientRect()
17-
let ripple = target.querySelector('.waves-ripple')
18-
if (!ripple) {
19-
ripple = document.createElement('span')
20-
ripple.className = 'waves-ripple'
21-
ripple.style.height = ripple.style.width = Math.max(rect.width, rect.height) + 'px'
22-
target.appendChild(ripple)
23-
} else {
24-
ripple.className = 'waves-ripple'
25-
}
26-
switch (opts.type) {
27-
case 'center':
28-
ripple.style.top = (rect.height / 2 - ripple.offsetHeight / 2) + 'px'
29-
ripple.style.left = (rect.width / 2 - ripple.offsetWidth / 2) + 'px'
30-
break
31-
default:
32-
ripple.style.top = (e.pageY - rect.top - ripple.offsetHeight / 2 - document.documentElement.scrollTop || document.body.scrollTop) + 'px'
33-
ripple.style.left = (e.pageX - rect.left - ripple.offsetWidth / 2 - document.documentElement.scrollLeft || document.body.scrollLeft) + 'px'
34-
}
35-
ripple.style.backgroundColor = opts.color
36-
ripple.className = 'waves-ripple z-active'
37-
return false
13+
},
14+
customOpts
15+
)
16+
const target = opts.ele
17+
if (target) {
18+
target.style.position = 'relative'
19+
target.style.overflow = 'hidden'
20+
const rect = target.getBoundingClientRect()
21+
let ripple = target.querySelector('.waves-ripple')
22+
if (!ripple) {
23+
ripple = document.createElement('span')
24+
ripple.className = 'waves-ripple'
25+
ripple.style.height = ripple.style.width = Math.max(rect.width, rect.height) + 'px'
26+
target.appendChild(ripple)
27+
} else {
28+
ripple.className = 'waves-ripple'
29+
}
30+
switch (opts.type) {
31+
case 'center':
32+
ripple.style.top = rect.height / 2 - ripple.offsetHeight / 2 + 'px'
33+
ripple.style.left = rect.width / 2 - ripple.offsetWidth / 2 + 'px'
34+
break
35+
default:
36+
ripple.style.top =
37+
(e.pageY - rect.top - ripple.offsetHeight / 2 - document.documentElement.scrollTop ||
38+
document.body.scrollTop) + 'px'
39+
ripple.style.left =
40+
(e.pageX - rect.left - ripple.offsetWidth / 2 - document.documentElement.scrollLeft ||
41+
document.body.scrollLeft) + 'px'
3842
}
39-
}, false)
43+
ripple.style.backgroundColor = opts.color
44+
ripple.className = 'waves-ripple z-active'
45+
return false
46+
}
47+
}
48+
49+
if (!el[context]) {
50+
el[context] = {
51+
removeHandle: handle
52+
}
53+
} else {
54+
el[context].removeHandle = handle
4055
}
56+
57+
return handle
4158
}
4259

60+
export default {
61+
bind(el, binding) {
62+
el.addEventListener('click', handleClick(el, binding), false)
63+
},
64+
update(el, binding) {
65+
el.removeEventListener('click', el[context].removeHandle, false)
66+
el.addEventListener('click', handleClick(el, binding), false)
67+
},
68+
unbind(el) {
69+
el.removeEventListener('click', el[context].removeHandle, false)
70+
el[context] = null
71+
delete el[context]
72+
}
73+
}

0 commit comments

Comments
 (0)