当前位置:首页 > VUE

vue实现动态高度抖动

2026-01-20 22:18:11VUE

实现动态高度抖动的方案

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

使用CSS关键帧动画

通过定义@keyframes创建抖动动画,动态绑定高度值:

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;
      let baseHeight = 100;
      let interval = setInterval(() => {
        this.currentHeight = baseHeight + Math.random() * 20 - 10;
      }, 50);

      setTimeout(() => {
        clearInterval(interval);
        this.currentHeight = baseHeight;
        this.isShaking = false;
      }, 1000);
    }
  }
}
</script>

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

使用GSAP动画库

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

import { gsap } from 'gsap';

methods: {
  gsapShake() {
    const el = this.$el.querySelector('.shake-box');
    gsap.to(el, {
      height: () => 100 + Math.random() * 30 - 15,
      duration: 0.1,
      repeat: 5,
      yoyo: true,
      onComplete: () => {
        gsap.to(el, { height: 100, duration: 0.2 });
      }
    });
  }
}

使用CSS自定义属性

结合CSS变量实现平滑过渡:

vue实现动态高度抖动

<template>
  <div 
    class="css-var-shake" 
    :style="{'--shake-height': heightVar}"
    @mouseenter="triggerShake"
  ></div>
</template>

<script>
export default {
  data() {
    return {
      heightVar: '100px'
    }
  },
  methods: {
    triggerShake() {
      const values = ['90px', '110px', '95px', '105px', '100px'];
      values.forEach((val, i) => {
        setTimeout(() => {
          this.heightVar = val;
        }, i * 100);
      });
    }
  }
}
</script>

<style>
.css-var-shake {
  width: 200px;
  height: var(--shake-height);
  background-color: #ff7043;
  transition: height 0.1s cubic-bezier(0.4, 0, 0.2, 1);
}
</style>

响应式高度抖动组件

创建可复用的抖动组件:

// ShakeHeight.vue
export default {
  props: {
    baseHeight: { type: Number, default: 100 },
    intensity: { type: Number, default: 10 },
    duration: { type: Number, default: 500 }
  },
  render() {
    return this.$scopedSlots.default({
      height: this.currentHeight,
      startShake: this.startShake
    });
  },
  data() {
    return {
      currentHeight: this.baseHeight
    }
  },
  methods: {
    startShake() {
      const startTime = Date.now();
      const animate = () => {
        const elapsed = Date.now() - startTime;
        const progress = elapsed / this.duration;

        if(progress < 1) {
          this.currentHeight = this.baseHeight + 
            Math.sin(progress * Math.PI * 10) * this.intensity;
          requestAnimationFrame(animate);
        } else {
          this.currentHeight = this.baseHeight;
        }
      };
      animate();
    }
  }
}

使用示例:

<ShakeHeight v-slot="{ height, startShake }">
  <div 
    :style="{ height: height + 'px' }" 
    @click="startShake"
    class="shake-item"
  ></div>
</ShakeHeight>

性能优化建议

  • 使用will-change: height提示浏览器优化
  • 避免在抖动期间触发重排操作
  • 对于复杂场景考虑使用Web Animation API
  • 移动端注意减少动画强度保证流畅性

以上方案可根据具体需求选择,CSS方案适合简单场景,GSAP适合复杂动画,组件化方案便于复用。

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

相关文章

vue实现动态按钮

vue实现动态按钮

实现动态按钮的基本思路 在Vue中实现动态按钮通常涉及根据数据状态动态改变按钮的样式、文本或行为。可以通过绑定动态类名、样式或事件来实现。 动态绑定按钮样式 使用v-bind:class或简写:cl…

vue 动态组件实现

vue 动态组件实现

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

vue实现动态表达

vue实现动态表达

Vue 动态表达的实现方法 在 Vue 中实现动态表达可以通过多种方式完成,以下是几种常见的实现方法: 使用计算属性(Computed Properties) 计算属性适用于需要基于响应式数据动态计…

vue实现动态卡片

vue实现动态卡片

实现动态卡片的基本思路 在Vue中实现动态卡片通常涉及数据绑定、循环渲染和交互逻辑。动态卡片的内容、样式或数量可以根据数据变化而更新。 数据驱动的卡片列表 使用v-for指令循环渲染卡片,数据存储在…

vue 实现动态菜单

vue 实现动态菜单

动态菜单的实现思路 在Vue中实现动态菜单通常涉及从后端API获取菜单数据,根据用户权限或角色动态渲染菜单项。核心思路是将菜单数据与组件分离,通过数据驱动视图。 数据准备与API交互 创建菜单数据模…

vue实现动态绘图

vue实现动态绘图

Vue 实现动态绘图的方法 使用 Canvas API 绘制动态图形 在 Vue 中可以通过 ref 获取 Canvas 元素,结合 JavaScript 的 Canvas API 实现动态绘图。创建…