当前位置:首页 > 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 样式:

vue实现点击涟漪

.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 涟漪效果库:

vue实现点击涟漪

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

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

    npm install vuetify

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

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

相关文章

vue底层实现

vue底层实现

Vue.js 的底层实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现细节: 响应式系统 Vue 2…

vue实现login

vue实现login

Vue 实现登录功能 在 Vue 中实现登录功能通常需要以下几个步骤: 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,包含用户名和密码输入框以及提交按钮。可以使用 Vue 的模板语法和双…

vue实现tip

vue实现tip

Vue实现Tooltip的方法 使用Vue实现Tooltip可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML的title属性 在Vue模板中直接使用HTML的title属性是最简单的实…

vue实现音乐

vue实现音乐

Vue实现音乐播放功能 使用Vue实现音乐播放功能可以通过多种方式完成,以下是几种常见的实现方法: 使用HTML5 Audio API 在Vue组件中直接使用HTML5的Audio对象来控制音乐播放…

vue 实现多选框

vue 实现多选框

Vue 实现多选框的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选框的选择功能。每个复选框的 value 属性会被添加到数组中,取消勾选时则从数…

vue实现输入地址

vue实现输入地址

Vue 实现输入地址功能 在 Vue 中实现输入地址功能,可以通过结合表单输入、地图 API(如高德、百度或 Google Maps)以及地址自动补全等方式完成。以下是几种常见方法: 使用高德地图…