当前位置:首页 > VUE

vue实现抖动

2026-01-07 21:09:03VUE

Vue 实现抖动效果

在 Vue 中实现抖动效果可以通过 CSS 动画或 JavaScript 动画库来实现。以下是几种常见的方法:

使用 CSS 动画

通过定义 @keyframes 动画并在 Vue 组件中应用:

<template>
  <div :class="{ 'shake': shouldShake }" @click="triggerShake">
    点击触发抖动
  </div>
</template>

<script>
export default {
  data() {
    return {
      shouldShake: false
    }
  },
  methods: {
    triggerShake() {
      this.shouldShake = true
      setTimeout(() => {
        this.shouldShake = false
      }, 500)
    }
  }
}
</script>

<style>
.shake {
  animation: shake 0.5s;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
  20%, 40%, 60%, 80% { transform: translateX(5px); }
}
</style>

使用 Vue Transition

Vue 的 <transition> 组件可以配合 CSS 实现抖动:

<template>
  <button @click="show = !show">
    点击触发
  </button>
  <transition name="shake">
    <div v-if="show" class="box">抖动内容</div>
  </transition>
</template>

<script>
export default {
  data() {
    return {
      show: false
    }
  }
}
</script>

<style>
.shake-enter-active {
  animation: shake 0.5s;
}
.shake-leave-active {
  animation: shake 0.5s reverse;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
  20%, 40%, 60%, 80% { transform: translateX(5px); }
}
</style>

使用第三方动画库

安装 animate.css 库:

npm install animate.css

在 Vue 中使用:

<template>
  <div class="animated" :class="{ 'shakeX': shouldShake }">
    抖动元素
  </div>
  <button @click="triggerShake">触发抖动</button>
</template>

<script>
import 'animate.css'
export default {
  data() {
    return {
      shouldShake: false
    }
  },
  methods: {
    triggerShake() {
      this.shouldShake = true
      setTimeout(() => {
        this.shouldShake = false
      }, 1000)
    }
  }
}
</script>

使用 JavaScript 实现

通过直接操作 DOM 元素的样式:

<template>
  <div ref="shakeElement" class="box">
    抖动内容
  </div>
  <button @click="shake">抖动</button>
</template>

<script>
export default {
  methods: {
    shake() {
      const element = this.$refs.shakeElement
      let distance = 5
      let times = 10
      let speed = 50

      for (let i = 0; i < times; i++) {
        setTimeout(() => {
          element.style.transform = `translateX(${distance}px)`
          distance = -distance
        }, i * speed)
      }
      setTimeout(() => {
        element.style.transform = 'translateX(0)'
      }, times * speed)
    }
  }
}
</script>

这些方法都可以在 Vue 中实现抖动效果,选择适合项目需求的方式即可。CSS 动画性能较好,适合简单效果;JavaScript 实现更灵活;第三方库则提供了更多预设动画。

vue实现抖动

标签: vue
分享给朋友:

相关文章

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &…

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存…

vue单选实现

vue单选实现

Vue 单选实现 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定一个变量,可以实现单选效果。当用户选择不同的选…