当前位置:首页 > VUE

vue实现动态高度抖动

2026-02-21 13:56:32VUE

Vue 实现动态高度抖动效果

动态高度抖动效果可以通过 CSS 动画和 Vue 的数据绑定结合实现。以下是几种实现方式:

使用 CSS 关键帧动画

定义关键帧动画,通过 Vue 动态绑定样式触发抖动:

vue实现动态高度抖动

<template>
  <div 
    class="shake-box" 
    :style="{ height: currentHeight + 'px' }"
    @click="startShake"
  ></div>
</template>

<script>
export default {
  data() {
    return {
      currentHeight: 100,
      isShaking: false
    }
  },
  methods: {
    startShake() {
      if (this.isShaking) return
      this.isShaking = true

      const originalHeight = this.currentHeight
      const shakeCount = 10
      let count = 0

      const shakeInterval = setInterval(() => {
        this.currentHeight = originalHeight + Math.random() * 20 - 10
        count++

        if (count >= shakeCount) {
          clearInterval(shakeInterval)
          this.currentHeight = originalHeight
          this.isShaking = false
        }
      }, 50)
    }
  }
}
</script>

<style>
.shake-box {
  width: 200px;
  background-color: #42b983;
  transition: height 0.05s ease-out;
}
</style>

使用 GSAP 动画库

GSAP 提供更强大的动画控制能力:

vue实现动态高度抖动

<template>
  <div ref="shakeElement" class="shake-box"></div>
  <button @click="triggerShake">触发抖动</button>
</template>

<script>
import { gsap } from 'gsap'

export default {
  methods: {
    triggerShake() {
      const el = this.$refs.shakeElement
      const originalHeight = 100

      gsap.to(el, {
        height: () => originalHeight + Math.random() * 30 - 15,
        duration: 0.05,
        repeat: 10,
        yoyo: true,
        onComplete: () => {
          gsap.to(el, { height: originalHeight, duration: 0.1 })
        }
      })
    }
  }
}
</script>

使用 CSS transform 实现视觉抖动

如果不需要实际改变元素高度,可以使用 transform 实现视觉抖动效果:

<template>
  <div 
    class="shake-transform" 
    :class="{ 'shake-active': isShaking }"
    @click="toggleShake"
  ></div>
</template>

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

<style>
.shake-transform {
  width: 200px;
  height: 100px;
  background-color: #42b983;
  transition: transform 0.1s;
}

.shake-active {
  animation: shake 0.5s cubic-bezier(.36,.07,.19,.97) both;
}

@keyframes shake {
  0%, 100% { transform: translateY(0); }
  20% { transform: translateY(-10px); }
  40% { transform: translateY(8px); }
  60% { transform: translateY(-6px); }
  80% { transform: translateY(4px); }
}
</style>

响应式高度抖动

结合 Vue 的响应式特性和 requestAnimationFrame 实现平滑抖动:

<template>
  <div 
    ref="shakeBox" 
    class="responsive-shake" 
    :style="{ height: height + 'px' }"
  ></div>
  <button @click="startResponsiveShake">开始抖动</button>
</template>

<script>
export default {
  data() {
    return {
      height: 100,
      animationId: null
    }
  },
  methods: {
    startResponsiveShake() {
      const startTime = Date.now()
      const duration = 1000
      const originalHeight = this.height

      const animate = () => {
        const elapsed = Date.now() - startTime
        const progress = Math.min(elapsed / duration, 1)

        if (progress < 1) {
          this.height = originalHeight + Math.sin(progress * Math.PI * 10) * 15
          this.animationId = requestAnimationFrame(animate)
        } else {
          this.height = originalHeight
          cancelAnimationFrame(this.animationId)
        }
      }

      animate()
    }
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId)
  }
}
</script>

以上方法可以根据具体需求选择,CSS 动画方案性能较好,GSAP 提供更精细的控制,而 requestAnimationFrame 方案则适合需要与其它动画同步的场景。

标签: 高度动态
分享给朋友:

相关文章

vue实现动态混入

vue实现动态混入

Vue 动态混入的实现方法 动态混入在 Vue 中可以通过编程方式将混入对象应用到组件实例,适合需要运行时决定混入逻辑的场景。 全局混入与局部混入 全局混入通过 Vue.mixin() 影响所有组件…

vue实现动态筛选

vue实现动态筛选

Vue实现动态筛选的方法 动态筛选是Vue应用中常见的功能需求,可以通过多种方式实现。以下是几种常用的实现方法: 使用计算属性实现筛选 计算属性是Vue中实现动态筛选的理想选择,它会根据依赖的数据自…

vue实现动态组件

vue实现动态组件

动态组件的基本概念 在Vue中,动态组件允许根据条件或用户交互切换不同的组件,无需重复编写模板逻辑。核心是通过<component>标签的is属性绑定组件名或组件对象。 使用is属性切换…

vue实现动态刷新数据

vue实现动态刷新数据

Vue 实现动态刷新数据的方法 使用计算属性(Computed Properties) 计算属性会根据依赖的响应式数据自动更新,适合基于现有数据动态计算的场景。 <template>…

vue实现文字动态输出

vue实现文字动态输出

Vue 实现文字动态输出 使用 v-html 指令动态渲染 HTML 在 Vue 中可以通过 v-html 指令动态渲染包含 HTML 标签的字符串。适用于需要动态插入带格式的文本场景。 <t…

vue实现动态高度抖动

vue实现动态高度抖动

实现动态高度抖动的方案 在Vue中实现动态高度抖动效果,可以通过CSS动画或JavaScript动画库实现。以下是几种常见方法: 使用CSS关键帧动画 通过定义@keyframes创建抖动动画,动态…