8c6157483f6155860f1dc3b50fb71269af774394.svn-base 2.2 KB

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