这次给大家带来bubbletransition如何实现页面切换功能,bubbletransition实现页面切换功能的注意事项有哪些,下面就是实战案例,一起来看一下。
codepen 地址
前端使用 spa 之后,能获得更多的控制权,比如页面切换动画,使用后端页面我们可能做不了上面的效果,或者做出来会出现明显的闪屏。因为所有资源都需要重新加载。
今天使用 vue,vue-router,animejs 来讲解如何上面的效果是如何实现的。
步骤
点击菜单,生成 bubble,开始执行入场动画
页面跳转
执行退场动画
函数式调用组件
我希望效果是通过一个对象去调用,而不是 v-show, v-if 之类的指令,并且为了保持统一,仍然使用 vue 来写组件。我通常会用新的 vue 根节点来实现,让效果独立于业务组件之外。
let instance = null
function createservices (comp) {
// ...
return new vue({
// ...
}).$children[0]
}
function getinstance () {
instance = instance || createservices(bubbletransitioncomponent)
return instance
}
const bubbletransition = {
scalein: () => {
return getinstance().animate('scalein')
},
fadeout: () => {
return getinstance().animate('fadeout')
}
}
接着实现 bubbletransitioncomponent,那么 bubbletransition.scalein, bubbletransition.scaleout 就能正常工作了。 animejs 可以监听的动画执行结束的事件。anime().finished 获得 promise 对象。
<template>
<p class="transition-bubble">
<span v-show="animating" class="bubble" id="bubble">
</span>
</p>
</template>
<script>
import anime from 'animejs'
export default {
name: 'transition-bubble',
data () {
return {
animating: false,
animeobjs: []
}
},
methods: {
scalein (selector = '#bubble', {duration = 800, easing = 'linear'} = {}) {
// this.animeobjs.push(anime().finished)
},
fadeout (selector = '#bubble', {duration = 300, easing = 'linear'} = {}) {
// ...
},
resetanimeobjs () {
this.animeobjs.reset()
this.animeobjs = []
},
animate (action, thenreset) {
return this[action]().then(() => {
this.resetanimeobjs()
})
}
}
}
最初的想法是,在 router config 里面给特定路由 meta 添加标记,beforeeach 的时候判断判断该标记执行动画。但是这种做法不够灵活,改成通过 hash 来标记,结合 vue-router,切换时重置 hash。
<router-link class="router-link" to="/#bubbletransition">home</router-link>
const bubble_transition_identifier = 'bubbletransition'
router.beforeeach((to, from, next) => {
if (to.hash.indexof(bubble_transition_identifier) > 0) {
const redirectto = object.assign({}, to)
redirectto.hash = ''
bubbletransition.scalein()
.then(() => next(redirectto))
} else {
next()
}
})
router.aftereach((to, from) => {
bubbletransition.fadeout()
})
酷炫的动画能在一瞬间抓住用户的眼球,我自己也经常在逛一些网站的时候发出,wocao,太酷了!!!的感叹。可能最终的实现用不了几行代码,自己多动手实现一下,下次设计师提出不合理的动画需求时可以装逼,这种效果我分分钟能做出来,但是我认为这里不应该使用 ** 动画,不符合用户的心理预期啊。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vue-i18n标准的使用步骤
vue无限加载vue-infinite-loading使用详解
vue-infinite-loading2.0使用说明
以上就是bubbletransition如何实现页面切换功能的详细内容。