当前位置:首页 > VUE

vue实现点击涟漪

2026-03-28 14:47:25VUE

Vue 实现点击涟漪效果

实现点击涟漪效果可以通过 CSS 和 Vue 的事件绑定结合完成。以下是两种常见的实现方式:

使用纯 CSS 和 Vue 事件

通过动态添加涟漪元素并利用 CSS 动画实现效果。

vue实现点击涟漪

<template>
  <div class="ripple-container" @click="createRipple">
    <slot></slot>
    <span class="ripple" :style="rippleStyle"></span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rippleStyle: {}
    }
  },
  methods: {
    createRipple(e) {
      const container = e.currentTarget
      const circle = document.createElement('span')
      const diameter = Math.max(container.clientWidth, container.clientHeight)
      const radius = diameter / 2

      this.rippleStyle = {
        width: `${diameter}px`,
        height: `${diameter}px`,
        left: `${e.clientX - container.getBoundingClientRect().left - radius}px`,
        top: `${e.clientY - container.getBoundingClientRect().top - radius}px`
      }

      setTimeout(() => {
        this.rippleStyle = {}
      }, 600)
    }
  }
}
</script>

<style>
.ripple-container {
  position: relative;
  overflow: hidden;
  display: inline-block;
}

.ripple {
  position: absolute;
  border-radius: 50%;
  transform: scale(0);
  animation: ripple 600ms linear;
  background-color: rgba(255, 255, 255, 0.7);
}

@keyframes ripple {
  to {
    transform: scale(4);
    opacity: 0;
  }
}
</style>

使用第三方库

可以使用现成的 Vue 涟漪效果库,如 vue-ripple-directive

安装:

vue实现点击涟漪

npm install vue-ripple-directive

使用:

import Vue from 'vue'
import Ripple from 'vue-ripple-directive'

Vue.directive('ripple', Ripple)
<button v-ripple>Click me</button>

自定义指令实现

创建自定义 Vue 指令实现更灵活的涟漪效果:

Vue.directive('ripple', {
  inserted(el) {
    el.style.position = 'relative'
    el.style.overflow = 'hidden'

    el.addEventListener('click', (e) => {
      const ripple = document.createElement('div')
      ripple.className = 'ripple-effect'

      const rect = el.getBoundingClientRect()
      const size = Math.max(rect.width, rect.height)
      const x = e.clientX - rect.left - size/2
      const y = e.clientY - rect.top - size/2

      ripple.style.width = ripple.style.height = `${size}px`
      ripple.style.left = `${x}px`
      ripple.style.top = `${y}px`

      el.appendChild(ripple)

      setTimeout(() => {
        ripple.remove()
      }, 600)
    })
  }
})
.ripple-effect {
  position: absolute;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.7);
  animation: ripple 600ms linear;
  transform: scale(0);
  pointer-events: none;
}

@keyframes ripple {
  to {
    transform: scale(4);
    opacity: 0;
  }
}

这些方法提供了不同复杂度的实现方案,可以根据项目需求选择最适合的方式。纯 CSS 方案轻量但功能有限,自定义指令方案更灵活,第三方库则提供了开箱即用的解决方案。

标签: 涟漪vue
分享给朋友:

相关文章

vue实现选择本地文件

vue实现选择本地文件

实现文件选择功能 在Vue中实现本地文件选择可以通过HTML原生的<input type="file">元素实现。创建一个基本的文件选择组件: <template> &l…

vue实现markdown

vue实现markdown

Vue 实现 Markdown 编辑器 在 Vue 中实现 Markdown 编辑器可以通过集成第三方库或自定义解析器完成。以下是两种常见方法: 使用第三方库(如 marked 或 markdown…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…