当前位置:首页 > 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 表格动态实现方法 动态表格的实现通常涉及数据绑定、列配置动态化以及交互功能的灵活处理。以下是几种常见场景的具体实现方式: 动态数据绑定 通过v-for指令循环渲染表格行数据,数据变化时表格自…

vue 动态组件实现

vue 动态组件实现

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

实现vue动态绑定

实现vue动态绑定

动态绑定的基本语法 在Vue中,使用v-bind指令或简写:实现动态绑定,将数据属性与HTML元素的属性关联起来。例如动态绑定class或style: <div v-bind:class="{…

vue实现动态打印

vue实现动态打印

Vue 实现动态打印的方法 动态打印通常指在网页上实时显示或输出内容,可以通过多种方式实现。以下是几种常见的实现方法: 使用 v-model 绑定数据 在 Vue 中,可以利用 v-model 实…

vue高度定位实现

vue高度定位实现

vue高度定位实现 在Vue中实现高度定位通常涉及CSS样式绑定、动态计算高度或使用第三方库。以下是几种常见方法: 固定高度定位 通过CSS直接设置固定高度,适用于静态布局: <templa…

vue实现动态路由

vue实现动态路由

动态路由的实现方式 Vue中实现动态路由通常涉及前端和后端的协作,以下是几种常见的实现方式: 基于用户权限的路由控制 在用户登录后,根据其权限动态生成路由表。使用router.addRoutes方…