当前位置:首页 > 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 的 v-model 实现表单双向绑定,创建动态内容输入框和图片上传组件。示例代码: <template> <di…

vue动态组件实现

vue动态组件实现

动态组件的基本用法 在Vue中,动态组件通过<component>标签和is属性实现。is属性可以绑定组件名称或组件选项对象,实现动态切换。 <template> <…

vue 动态组件实现

vue 动态组件实现

vue 动态组件实现 Vue 的动态组件功能允许根据条件或用户交互动态切换不同的组件,主要通过 <component> 标签和 is 属性实现。 基本用法 通过 is 属性绑定组件名或组…

vue实现动态导航

vue实现动态导航

Vue 实现动态导航的方法 动态导航通常指根据用户权限、数据变化或交互行为实时更新的导航菜单。以下是几种常见实现方式: 基于路由配置的动态导航 通过 Vue Router 的路由配置生成导航菜单…

vue实现动态浮出

vue实现动态浮出

Vue 实现动态浮出效果 动态浮出效果通常指根据用户交互(如点击、悬停)动态显示隐藏的浮层(如弹窗、菜单、提示框)。以下是几种实现方式: 使用 v-show 或 v-if 控制显示隐藏 通过 V…

vue实现动态columns

vue实现动态columns

Vue 实现动态 columns 的方法 使用 v-for 动态渲染表格列 通过 v-for 指令可以动态渲染表格的列,columns 数据可以来自组件的 props 或 data。 <te…