本文主要是介绍Vue_button实现水波纹效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录结构:
和调用方法
index.js:
import waves from './waves'const install = function(Vue) {Vue.directive('waves', waves)
}if (window.Vue) {window.waves = wavesVue.use(install); // eslint-disable-line
}waves.install = install
export default waves
waves.js:
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.eleif (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'breakdefault: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.colorripple.className = 'waves-ripple z-active'return false}}if (!el[context]) {el[context] = {removeHandle: handle}} else {el[context].removeHandle = handle}return handle
}export default {bind(el, binding) {el.addEventListener('click', handleClick(el, binding), false)},update(el, binding) {el.removeEventListener('click', el[context].removeHandle, false)el.addEventListener('click', handleClick(el, binding), false)},unbind(el) {el.removeEventListener('click', el[context].removeHandle, false)el[context] = nulldelete el[context]}
}
waves.css:
.waves-ripple {position: absolute;border-radius: 100%;background-color: rgba(0, 0, 0, 0.15);background-clip: padding-box;pointer-events: none;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;-webkit-transform: scale(0);-ms-transform: scale(0);transform: scale(0);opacity: 1;
}.waves-ripple.z-active {opacity: 0;-webkit-transform: scale(2);-ms-transform: scale(2);transform: scale(2);-webkit-transition: opacity 1.2s ease-out, -webkit-transform 0.6s ease-out;transition: opacity 1.2s ease-out, -webkit-transform 0.6s ease-out;transition: opacity 1.2s ease-out, transform 0.6s ease-out;transition: opacity 1.2s ease-out, transform 0.6s ease-out, -webkit-transform 0.6s ease-out;
}
这篇关于Vue_button实现水波纹效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!