当前位置:首页 > VUE

vue实现点击涟漪

2026-01-15 01:33:14VUE

Vue 实现点击涟漪效果

在 Vue 中实现点击涟漪效果可以通过自定义指令或组件的方式完成。以下是两种常见实现方法:

使用自定义指令

创建一个自定义指令 v-ripple,动态生成涟漪元素并添加动画效果:

// 在 main.js 或单独指令文件中
Vue.directive('ripple', {
  bind(el, binding) {
    el.style.position = 'relative'
    el.style.overflow = 'hidden'

    el.addEventListener('click', (e) => {
      const ripple = document.createElement('span')
      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)
    })
  }
})

添加对应的 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(2);
    opacity: 0;
  }
}

使用组件方式

创建可复用的 Ripple 组件:

<template>
  <div 
    class="ripple-container"
    @click="createRipple"
    ref="container"
  >
    <slot></slot>
    <span 
      v-for="(ripple, index) in ripples"
      :key="index"
      class="ripple"
      :style="{
        left: ripple.x + 'px',
        top: ripple.y + 'px',
        width: ripple.size + 'px',
        height: ripple.size + 'px'
      }"
    ></span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ripples: []
    }
  },
  methods: {
    createRipple(e) {
      const container = this.$refs.container
      const rect = container.getBoundingClientRect()
      const size = Math.max(rect.width, rect.height)

      this.ripples.push({
        x: e.clientX - rect.left - size/2,
        y: e.clientY - rect.top - size/2,
        size: size
      })

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

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

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

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

使用第三方库

对于更复杂的需求,可以考虑使用现成的 Vue 涟漪效果库:

  1. vue-ripple-directive:轻量级的涟漪指令

    npm install vue-ripple-directive
  2. vuetify:Material Design 组件库内置涟漪效果

    vue实现点击涟漪

    npm install vuetify

这些实现方法都可以根据项目需求调整涟漪颜色、大小和动画持续时间等参数。自定义指令方式适合快速添加基本效果,组件方式提供更好的封装性,而第三方库则提供最完整的解决方案。

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

相关文章

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue原型实现

vue原型实现

Vue 原型实现的关键点 Vue.js 的原型系统是其响应式机制的核心部分,通过原型链实现数据的动态绑定和依赖收集。以下是实现 Vue 原型的主要技术要点。 数据响应式原理 Vue 通过 Objec…

vue 实现滚动

vue 实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以通…

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue课程实现

vue课程实现

Vue 课程实现方案 课程内容设计 Vue课程应从基础到高级逐步展开,涵盖Vue核心概念、组件化开发、状态管理、路由、构建工具等。基础部分包括Vue实例、模板语法、计算属性和侦听器。中级部分深入组件通…