当前位置:首页 > VUE

vue实现动态高度抖动

2026-01-20 22:18:11VUE

实现动态高度抖动的方案

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

使用CSS关键帧动画

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

<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变量实现平滑过渡:

<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实现动态用户表格

Vue 动态用户表格实现方法 基础表格结构 使用<table>或第三方组件库(如Element UI的el-table)构建基础框架。静态数据示例: <template>…

vue实现微博发布动态

vue实现微博发布动态

Vue 实现微博发布动态功能 数据绑定与表单设计 使用 Vue 的 v-model 实现表单双向绑定,创建动态内容输入框和图片上传组件。示例代码: <template> <d…

vue 实现动态路由

vue 实现动态路由

动态路由的实现方法 Vue中实现动态路由通常涉及以下关键步骤,结合Vue Router的API和异步加载逻辑完成。 基础配置 安装Vue Router并初始化路由实例。在router/index.j…

vue表格动态实现

vue表格动态实现

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

vue实现动态时间

vue实现动态时间

Vue 实现动态时间的常见方法 使用 setInterval 更新数据 在 Vue 组件的 data 中定义时间变量,通过 setInterval 定时更新。组件销毁时需清除定时器避免内存泄漏。 e…

vue实现动态按钮

vue实现动态按钮

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