blob: ac1d8611ac0c733477d7bb9f8e459397b62a9ff1 [file] [log] [blame]
huibing.xie1f1606f2018-08-20 15:46:55 +08001import './waves.css'
2
3export default{
4 bind(el, binding) {
5 el.addEventListener('click', e => {
6 const customOpts = Object.assign({}, binding.value)
7 const opts = Object.assign({
8 ele: el, // 波纹作用元素
9 type: 'hit', // hit点击位置扩散center中心点扩展
10 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.body.scrollTop) + 'px'
33 ripple.style.left = (e.pageX - rect.left - ripple.offsetWidth / 2 - document.body.scrollLeft) + 'px'
34 }
35 ripple.style.backgroundColor = opts.color
36 ripple.className = 'waves-ripple z-active'
37 return false
38 }
39 }, false)
40 }
41}
42