el-button加上涟漪动画效果
时间: 2023-09-02 18:12:26 浏览: 198
element-ui 的el-button组件中添加自定义颜色和图标的实现方法
要给`el-button`加上涟漪动画效果,需要使用`waves`指令来实现。具体步骤如下:
1. 首先,需要在`<template>`标签中引入`waves.css`文件,该文件用于控制涟漪动画效果的样式,代码如下:
```html
<template>
<div>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/waves.css">
<el-button v-waves>点击我</el-button>
</div>
</template>
```
2. 然后,在`<script>`标签中注册`waves`指令,代码如下:
```javascript
export default {
directives: {
waves: {
bind(el, binding) {
el.addEventListener('mousedown', 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) + 'px'
ripple.style.left = (e.pageX - rect.left - ripple.offsetWidth / 2 - document.documentElement.scrollLeft) + 'px'
}
ripple.style.backgroundColor = opts.color
ripple.className = 'waves-ripple z-active'
return false
}
}, false)
}
}
}
}
```
3. 最后,在`<el-button>`标签中添加`v-waves`指令即可,代码如下:
```html
<template>
<div>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/waves.css">
<el-button v-waves>点击我</el-button>
</div>
</template>
<script>
export default {
directives: {
waves: {
bind(el, binding) {
el.addEventListener('mousedown', 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) + 'px'
ripple.style.left = (e.pageX - rect.left - ripple.offsetWidth / 2 - document.documentElement.scrollLeft) + 'px'
}
ripple.style.backgroundColor = opts.color
ripple.className = 'waves-ripple z-active'
return false
}
}, false)
}
}
}
}
</script>
```
这样就可以给`el-button`添加涟漪动画效果了。
阅读全文