|
1 | 1 | import './waves.css'
|
2 | 2 |
|
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 | + { |
8 | 10 | ele: el, // 波纹作用元素
|
9 | 11 | type: 'hit', // hit 点击位置扩散 center中心点扩展
|
10 | 12 | 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' |
38 | 42 | }
|
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 |
40 | 55 | }
|
| 56 | + |
| 57 | + return handle |
41 | 58 | }
|
42 | 59 |
|
| 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