当前位置:首页 > 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实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="w…

vue动态组件实现

vue动态组件实现

Vue 动态组件实现方法 使用 component 标签与 is 属性 通过 Vue 内置的 <component> 标签和 is 属性实现动态组件切换。is 的值可以是已注册的组件名或导…

vue实现动态显示布局

vue实现动态显示布局

Vue 实现动态显示布局的方法 动态显示布局通常涉及根据数据或用户交互动态调整页面结构和内容。以下是几种常见的实现方式: 使用 v-if 和 v-show 指令 v-if 和 v-show 都可以…

vue实现动态显示页面

vue实现动态显示页面

动态显示页面的实现方法 在Vue中实现动态显示页面通常涉及数据绑定、条件渲染、列表渲染以及动态组件等技术。以下是几种常见的实现方式: 数据驱动视图 Vue的核心思想是数据驱动视图,通过修改数据自动…

jquery 高度

jquery 高度

jQuery 获取和设置元素高度的方法 使用 jQuery 可以方便地获取或设置元素的高度。以下是几种常用的方法: 获取元素高度 使用 height() 方法可以获取匹配元素中第一个元素的计算高度(…

vue实现动态禁用

vue实现动态禁用

Vue 实现动态禁用的方法 在 Vue 中,可以通过多种方式实现动态禁用元素或组件。以下是几种常见的方法: 使用 v-bind 绑定 disabled 属性 通过 v-bind 动态绑定 disa…