【动画封装(不带效果)】
<html>
<head>
<meta charset="utf-8" />
<title>Vue中的动画封装</title>
<script src="./vue.js"></script>
<style>
.v-enter, .v-leave {
opacity: 0
}
.v-enter-active, .v-leave-active {
transition: opacity 1s
}
</style>
</head>
<body>
<div id="root">
<fade :show="show">
<div>hellow world</div>
</fade>
<fade :show="show">
<h1>hellow world</h1>
</fade>
<button @click="handleBtnClick">toggle</button>
</div>
<script>
//封装
Vue.component('fade', {
props: ['show'],
template: '<transition><slot v-if="show"></slot></transition> '
})
var vm = new Vue({
el: "#root",
data: {
show: true
},
methods: {
handleBtnClick: function () {
this.show = !this.show
},
}
})
</script>
</body>
</html>
动画封装(效果单独 )
【动画封装(效果一起)】
<html>
<head>
<meta charset="utf-8" />
<title>Vue中的动画封装</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="root">
<fade :show="show">
<div>hellow world</div>
</fade>
<fade :show="show">
<h1>hellow world</h1>
</fade>
<button @click="handleBtnClick">toggle</button>
</div>
<script>
//封装
Vue.component('fade', {
props: ['show'],
template: '<transition @before-enter="handleBeforeEnter" @enter="handleEnter"><slot v-if="show"></slot></transition>',
methods: {
handleBeforeEnter: function (el) {
el.style.color = 'red'
},
handleEnter: function (el, done) {
setTimeout(() => {
el.style.color = 'green'
done()
}, 2000)
}
}
})
var vm = new Vue({
el: "#root",
data: {
show: true
},
methods: {
handleBtnClick: function () {
this.show = !this.show
},
}
})
</script>
</body>
</html>
动画封装(效果一起)
动画封装(效果一起)
网友评论