vue实现jquery+animate
在Vue中实现类似jQuery的动画效果,可以通过Vue的内置过渡系统或第三方库完成。以下是几种常见方法:
使用Vue过渡和动画
Vue提供了<transition>和<transition-group>组件,结合CSS实现动画效果。

<template>
<button @click="show = !show">Toggle</button>
<transition name="fade">
<p v-if="show">Hello Vue!</p>
</transition>
</template>
<script>
export default {
data() {
return { show: true }
}
}
</script>
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
</style>
使用JavaScript钩子
通过Vue过渡的JavaScript钩子实现更复杂的动画逻辑。
<template>
<button @click="show = !show">Toggle</button>
<transition
@before-enter="beforeEnter"
@enter="enter"
@leave="leave"
>
<p v-if="show">Custom Animation</p>
</transition>
</template>
<script>
export default {
data() {
return { show: true }
},
methods: {
beforeEnter(el) {
el.style.opacity = 0
},
enter(el, done) {
let opacity = 0
const interval = setInterval(() => {
opacity += 0.1
el.style.opacity = opacity
if (opacity >= 1) {
clearInterval(interval)
done()
}
}, 50)
},
leave(el, done) {
let opacity = 1
const interval = setInterval(() => {
opacity -= 0.1
el.style.opacity = opacity
if (opacity <= 0) {
clearInterval(interval)
done()
}
}, 50)
}
}
}
</script>
使用GSAP库
GSAP是专业的动画库,可以替代jQuery的动画功能。

<template>
<button @click="animateBox">Animate</button>
<div ref="box" class="box"></div>
</template>
<script>
import { gsap } from 'gsap'
export default {
methods: {
animateBox() {
gsap.to(this.$refs.box, {
duration: 1,
x: 100,
rotation: 360,
ease: "bounce.out"
})
}
}
}
</script>
<style>
.box {
width: 50px;
height: 50px;
background: #42b983;
}
</style>
使用Animate.css
结合Animate.css快速实现预设动画效果。
<template>
<button @click="show = !show">Toggle</button>
<transition
enter-active-class="animate__animated animate__bounceIn"
leave-active-class="animate__animated animate__bounceOut"
>
<p v-if="show">Animate.css Example</p>
</transition>
</template>
<script>
export default {
data() {
return { show: true }
}
}
</script>
使用Velocity.js
Velocity.js是高性能的动画引擎,语法与jQuery类似。
<template>
<button @click="animate">Animate</button>
<div ref="box" class="box"></div>
</template>
<script>
import Velocity from 'velocity-animate'
export default {
methods: {
animate() {
Velocity(this.$refs.box,
{ width: "200px", opacity: 0.5 },
{ duration: 1000 }
)
}
}
}
</script>
<style>
.box {
width: 50px;
height: 50px;
background: #42b983;
}
</style>
这些方法提供了从简单到复杂的动画实现方案,可以根据项目需求选择合适的方案。Vue的过渡系统适合基础动画,而GSAP或Velocity.js更适合复杂交互式动画。






