Skip to content

Instantly share code, notes, and snippets.

@magicianShiro
Created November 1, 2018 10:55
Show Gist options
  • Save magicianShiro/d7e61e18cc794ea42266ef45232fe8c7 to your computer and use it in GitHub Desktop.
Save magicianShiro/d7e61e18cc794ea42266ef45232fe8c7 to your computer and use it in GitHub Desktop.
vue 水波纹指令
export default {
inserted (el, binding, vnode) {
vnode.context.$nextTick(() => {
let ripple = new Ripple({ el, binding })
ripple.addEvent()
})
},
update (el, binding, vnode) {
vnode.context.$nextTick(() => {
let ripple = new Ripple({ el, binding })
ripple.addEvent()
})
}
}
let ripple
function Ripple (option) {
if (!ripple) {
ripple = new Factory(option)
} else {
ripple.refresh(option)
}
return ripple
}
class Factory {
constructor (option) {
this.el = option.el
this.binding = option.binding
}
refresh (option) {
this.el = option.el
this.binding = option.binding
}
addEvent () {
this.el.style.position = 'relative'
this.el.style.overflow = 'hidden'
this.el.addEventListener('click', this.addRippleEffect)
}
addRippleEffect (e) {
var target = e.target
var rect = target.getBoundingClientRect()
var ripple = target.querySelector('.ripple')
if (!ripple) {
ripple = document.createElement('span')
ripple.className = 'ripple'
ripple.style.height = ripple.style.width = Math.max(rect.width, rect.height) + 'px'
target.appendChild(ripple)
}
ripple.classList.remove('show')
var top = e.pageY - rect.top - ripple.offsetHeight / 2 - document.body.scrollTop
var left = e.pageX - rect.left - ripple.offsetWidth / 2 - document.body.scrollLeft
ripple.style.top = top + 'px'
ripple.style.left = left + 'px'
ripple.classList.add('show')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment