- 通过Vue中的js动画生命周期函数实现动画效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>22Vue中的Js动画与Velocityjs的结合.html</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
<transition name="fade" @before-enter="handleBeforeEnter" @enter="handleEnter" @after-enter="handleAfterEnter">
<div v-show="show">hello world</div>
</transition>
<button @click="show = !show">change</button>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
show: true
},
methods: {
handleBeforeEnter: function (el) {
el.style.color = "red"
},
handleEnter: function (el, done) {
setTimeout(() => {
el.style.color = "green"
}, 2000)
setTimeout(() => {
done()
}, 4000)
},
handleAfterEnter: function (el) {
el.style.color = "black"
}
},
})
</script>
</body>
</html>
js常用动画库 velocity.js
velocity.js 官网
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>22Vue中的Js动画与Velocityjs的结合.html</title>
<script src="./vue.js"></script>
<script src="https://raw.githubusercontent.com/julianshapiro/velocity/master/velocity.min.js"></script>
</head>
<body>
<div id="app">
<transition name="fade" @before-enter="handleBeforeEnter" @enter="handleEnter" @after-enter="handleAfterEnter">
<div v-show="show">hello world</div>
</transition>
<button @click="show = !show">change</button>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
show: true
},
methods: {
handleBeforeEnter: function (el) {
el.style.opacity = 0
},
handleEnter: function (el, done) {
Velocity(el, { opacity: 1 }, { duration: 1000, complete: done })
},
handleAfterEnter: function (el) {
console.log('动画结束');
}
},
})
</script>
</body>
</html>
网友评论