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

vue实现抖动

<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实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ { ti…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…

甘特图vue实现

甘特图vue实现

甘特图 Vue 实现方法 使用开源库 vue-ganttastic 安装依赖: npm install vue-ganttastic 基础实现代码: <template> <…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue 实现$(id)

vue 实现$(id)

在 Vue 中实现类似 jQuery 的 $(id) 功能,可以通过以下几种方式操作 DOM 元素: 使用 ref 属性 Vue 推荐使用 ref 属性来直接访问 DOM 元素或组件实例。在模板中为…