当前位置:首页 > 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 动态用户表格实现方法 基础表格结构 使用<table>或第三方组件库(如Element UI的el-table)构建基础框架。静态数据示例: <template>…

elementui动态

elementui动态

ElementUI 动态组件与数据绑定 ElementUI 支持动态渲染组件和数据绑定,常用于表格、表单等场景的动态生成。通过 v-for 和动态属性绑定实现组件灵活配置。 动态生成表格列 <…

vue动态表格实现

vue动态表格实现

Vue动态表格实现方法 使用v-for指令渲染动态数据 通过v-for指令可以循环遍历数组数据生成表格行,数据变化时表格自动更新。示例代码: <template> <table…

vue实现数据动态

vue实现数据动态

Vue 实现数据动态绑定的方法 Vue.js 的核心特性之一是数据绑定,通过响应式系统实现数据的动态更新。以下是几种常见的实现方式: 双向数据绑定(v-model) 使用 v-model 指令可以实…

vue实现动态按钮

vue实现动态按钮

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

vue实现动态表头

vue实现动态表头

实现动态表头的方案 在Vue中实现动态表头通常需要结合数据驱动和组件化思想。以下是几种常见方法: 基于v-for渲染表头 通过v-for指令循环渲染表头列,数据源可以是数组或对象: <tem…