当前位置:首页 > VUE

vue实现点击涟漪

2026-03-08 04:17:42VUE

实现点击涟漪效果的方法

在Vue中实现点击涟漪效果可以通过多种方式完成,以下是几种常见的方法:

使用CSS动画 通过CSS的animationtransform属性可以创建简单的涟漪效果。在Vue模板中添加一个元素,当点击时动态添加类名触发动画。

<template>
  <div class="ripple-container" @click="createRipple">
    <span class="ripple" :class="{ 'animate': isRippling }"></span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isRippling: false
    }
  },
  methods: {
    createRipple() {
      this.isRippling = true
      setTimeout(() => {
        this.isRippling = false
      }, 600)
    }
  }
}
</script>

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

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

.animate {
  animation: ripple 0.6s linear;
}

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

使用第三方库 可以使用现成的Vue涟漪效果库,如vue-ripple-directivev-ripple,这些库提供了更丰富的配置选项和更好的兼容性。

npm install v-ripple
import Vue from 'vue'
import VRipple from 'v-ripple'

Vue.use(VRipple)
<template>
  <button v-ripple>Click Me</button>
</template>

动态计算涟漪位置 如果需要根据点击位置生成涟漪,可以通过JavaScript动态计算位置并创建元素。

<template>
  <div class="ripple-container" @click="createRipple">
    <slot></slot>
  </div>
</template>

<script>
export default {
  methods: {
    createRipple(event) {
      const container = event.currentTarget
      const ripple = document.createElement('span')
      ripple.classList.add('ripple')
      container.appendChild(ripple)

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

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

      setTimeout(() => {
        ripple.remove()
      }, 600)
    }
  }
}
</script>

<style>
.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(4);
    opacity: 0;
  }
}
</style>

自定义指令实现 可以创建自定义Vue指令来实现更灵活的涟漪效果。

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 = `${size}px`
      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);
  transform: scale(0);
  animation: ripple 0.6s linear;
}

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

以上方法可以根据项目需求选择适合的实现方式,CSS动画适合简单场景,而自定义指令或第三方库则提供了更多功能和灵活性。

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

相关文章

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue 实现筛选

vue 实现筛选

实现筛选功能的基本方法 在Vue中实现筛选功能通常涉及以下几个核心步骤: 数据绑定与筛选逻辑 使用v-model绑定筛选条件到Vue实例的数据属性,结合计算属性实现动态筛选: data() {…

vue实现树形

vue实现树形

Vue 实现树形组件 在 Vue 中实现树形结构通常需要递归组件和动态数据绑定。以下是实现树形组件的几种常见方法。 递归组件实现 递归组件是 Vue 中实现树形结构的核心方法。通过组件调用自身,可以…

vue app 实现

vue app 实现

Vue App 实现步骤 创建Vue项目 使用Vue CLI或Vite初始化项目。Vue CLI适合传统项目,Vite适合现代轻量级应用。 npm create vue@latest my-vu…