当前位置:首页 > VUE

vue水波球实现

2026-01-16 06:26:52VUE

Vue 水波球实现方法

水波球(Ripple Effect)是一种常见的交互效果,当用户点击或触摸元素时,会产生类似水波纹的扩散效果。以下是几种实现方式:

使用 CSS 和 Vue 事件

通过 Vue 的事件绑定和 CSS 动画实现基础水波效果:

<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水波球实现

  1. 安装 vue-ripple-directive

    npm install vue-ripple-directive
  2. 在 Vue 项目中使用:

    vue水波球实现

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

Vue.directive('ripple', Ripple)


3. 在模板中应用:
```html
<button v-ripple>Click Me</button>

自定义指令实现

创建自定义 Vue 指令实现更灵活的控制:

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

    el.addEventListener('click', (e) => {
      const circle = document.createElement('span')
      const diameter = Math.max(el.clientWidth, el.clientHeight)
      const radius = diameter / 2

      circle.style.width = circle.style.height = `${diameter}px`
      circle.style.left = `${e.clientX - el.getBoundingClientRect().left - radius}px`
      circle.style.top = `${e.clientY - el.getBoundingClientRect().top - radius}px`
      circle.classList.add('ripple-effect')

      const existingRipple = el.querySelector('.ripple-effect')
      if (existingRipple) {
        existingRipple.remove()
      }

      el.appendChild(circle)

      setTimeout(() => {
        circle.remove()
      }, 600)
    })
  }
})

配套 CSS:

.ripple-effect {
  position: absolute;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.7);
  transform: scale(0);
  animation: ripple 0.6s linear;
  pointer-events: none;
}

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

注意事项

  1. 性能优化:避免频繁创建和移除 DOM 元素,可以复用已有的波纹元素
  2. 移动端适配:考虑添加 touchstart 事件支持移动设备
  3. 颜色自定义:通过 props 或 CSS 变量允许自定义波纹颜色
  4. 无障碍访问:确保效果不会干扰键盘导航和屏幕阅读器

以上方法提供了从简单到复杂的不同实现方案,可根据项目需求选择合适的实现方式。

标签: 水波vue
分享给朋友:

相关文章

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实现…

vue实现验证

vue实现验证

Vue 表单验证实现方法 在Vue中实现表单验证可以通过多种方式完成,以下是常见的几种方法: 使用Vuelidate库 Vuelidate是一个轻量级的Vue表单验证库,安装后可以通过简单的配置实现…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装依…

vue实现刷新

vue实现刷新

Vue 实现页面刷新的方法 在Vue中实现页面刷新可以通过多种方式,以下是几种常见的方法: 使用 window.location.reload() 通过调用浏览器的原生方法强制刷新当前页面:…

vue实现dialog

vue实现dialog

Vue 实现 Dialog 的方法 使用 Vue 原生组件实现 Vue 可以通过组件化的方式实现 Dialog,以下是一个简单的实现示例: <template> <div>…