vue实现回弹效果
Vue 实现回弹效果的方法
回弹效果(Bounce Effect)常见于移动端交互,可以通过 CSS 动画或 JavaScript 实现。以下是几种在 Vue 中实现回弹效果的方案:
使用 CSS 动画实现基础回弹
通过 @keyframes 定义回弹动画,结合 Vue 的 v-bind:class 或 v-bind:style 动态触发动画。
<template>
<div
class="box"
@click="toggleBounce"
:class="{ 'bounce': isBouncing }"
>
点击回弹
</div>
</template>
<script>
export default {
data() {
return { isBouncing: false };
},
methods: {
toggleBounce() {
this.isBouncing = true;
setTimeout(() => this.isBouncing = false, 1000);
}
}
};
</script>
<style>
.box {
width: 100px;
height: 100px;
background: #42b983;
transition: transform 0.3s;
}
.bounce {
animation: bounce 0.5s;
}
@keyframes bounce {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.2); }
}
</style>
使用第三方库(如 GSAP)
GSAP 提供更精细的动画控制,适合复杂回弹效果。
<template>
<div ref="box" class="box" @click="bounce">
高级回弹
</div>
</template>
<script>
import { gsap } from 'gsap';
export default {
methods: {
bounce() {
gsap.to(this.$refs.box, {
y: -20,
duration: 0.3,
ease: "bounce.out",
yoyo: true,
repeat: 1
});
}
}
};
</script>
结合 Touch 事件实现拖拽回弹
适用于可拖拽元素释放后的回弹效果。
<template>
<div
ref="draggable"
class="draggable"
@touchstart="startDrag"
@touchmove="onDrag"
@touchend="endDrag"
></div>
</template>
<script>
export default {
data() {
return { startY: 0, currentY: 0, isDragging: false };
},
methods: {
startDrag(e) {
this.startY = e.touches[0].clientY;
this.isDragging = true;
},
onDrag(e) {
if (!this.isDragging) return;
this.currentY = e.touches[0].clientY - this.startY;
this.$refs.draggable.style.transform = `translateY(${this.currentY}px)`;
},
endDrag() {
this.isDragging = false;
this.$refs.draggable.style.transition = 'transform 0.5s ease-out';
this.$refs.draggable.style.transform = 'translateY(0)';
setTimeout(() => {
this.$refs.draggable.style.transition = '';
}, 500);
}
}
};
</script>
动态调整贝塞尔曲线
通过 cubic-bezier 自定义回弹动画曲线。

.bounce-effect {
transition: transform 0.6s cubic-bezier(0.68, -0.6, 0.32, 1.6);
}
.bounce-effect:active {
transform: scale(0.9);
}
注意事项
- 性能优化:避免频繁触发重排(如修改
width/height),优先使用transform和opacity。 - 移动端适配:使用
touch事件替代click以减少延迟。 - 动画重置:通过
setTimeout或transitionend事件重置动画状态。






