鸿蒙智能电子锁前端项目
whychdw
2024-10-09 1f14aca3e26ea960b8d20bb83d81c31339b7f48c
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
import './waves.css';
 
const context = '@@wavesContext';
 
function handleClick(el, binding) {
  function handle(e) {
    const customOpts = Object.assign({}, binding.value);
    const opts = Object.assign({
      ele: el, // 波纹作用元素
      type: 'hit', // hit 点击位置扩散 center中心点扩展
      color: 'rgba(0, 0, 0, 0.15)' // 波纹颜色
    },
    customOpts
    );
    const target = opts.ele;
    if (target) {
      target.style.position = 'relative';
      target.style.overflow = 'hidden';
      const rect = target.getBoundingClientRect();
      let ripple = target.querySelector('.waves-ripple');
      if (!ripple) {
        ripple = document.createElement('span');
        ripple.className = 'waves-ripple';
        ripple.style.height = ripple.style.width = Math.max(rect.width, rect.height) + 'px';
        target.appendChild(ripple);
      } else {
        ripple.className = 'waves-ripple';
      }
      switch (opts.type) {
        case 'center':
          ripple.style.top = rect.height / 2 - ripple.offsetHeight / 2 + 'px';
          ripple.style.left = rect.width / 2 - ripple.offsetWidth / 2 + 'px';
          break;
        default:
          ripple.style.top =
            (e.pageY - rect.top - ripple.offsetHeight / 2 - document.documentElement.scrollTop ||
              document.body.scrollTop) + 'px';
          ripple.style.left =
            (e.pageX - rect.left - ripple.offsetWidth / 2 - document.documentElement.scrollLeft ||
              document.body.scrollLeft) + 'px';
      }
      ripple.style.backgroundColor = opts.color;
      ripple.className = 'waves-ripple z-active';
      return false;
    }
  }
 
  if (!el[context]) {
    el[context] = {
      removeHandle: handle
    };
  } else {
    el[context].removeHandle = handle;
  }
 
  return handle;
}
 
function loopFindParentButton(el) {
  if (!el) return null;
  if (el.classList.contains('el-button')) {
    return el;
  }
  return loopFindParentButton(el.parentElement);
}
 
export default {
  mounted(textEl, binding) {
    const el = loopFindParentButton(textEl);
    el.addEventListener('click', handleClick(el, binding), false);
  },
  updated(textEl, binding) {
    const el = loopFindParentButton(textEl);
    el.removeEventListener('click', el[context].removeHandle, false);
    el.addEventListener('click', handleClick(el, binding), false);
  },
  unmounted(textEl) {
    const el = loopFindParentButton(textEl);
    el.removeEventListener('click', el[context].removeHandle, false);
    el[context] = null;
    delete el[context];
  }
};