|
| 1 | +import { |
| 2 | + back, |
| 3 | + useTransitionEnter, |
| 4 | + useTransitionLeave, |
| 5 | + useProgressExitAnimated, |
| 6 | +} from '@0x30/vue-navigation' |
| 7 | +import anime from 'animejs' |
| 8 | +import { defineComponent } from 'vue' |
| 9 | +import Page from '../Page' |
| 10 | + |
| 11 | +const PageBodyClassName = 'vue-navigation-layout-body' |
| 12 | + |
| 13 | +const NavPage = defineComponent({ |
| 14 | + name: 'NavPage', |
| 15 | + setup: (props, { slots }) => { |
| 16 | + const animeProps = { duration: 500, easing: 'easeOutExpo' } |
| 17 | + |
| 18 | + useTransitionEnter(({ to, from }, complete) => { |
| 19 | + const an = anime.timeline({ ...animeProps, complete }) |
| 20 | + an.add({ targets: to, translateX: ['100%', '0'] }) |
| 21 | + if (from?.classList.contains(PageBodyClassName)) { |
| 22 | + an.add( |
| 23 | + { |
| 24 | + targets: from, |
| 25 | + translateX: ['0%', '-50%'], |
| 26 | + complete: (anim) => anim.seek(0), |
| 27 | + }, |
| 28 | + 0 |
| 29 | + ) |
| 30 | + } |
| 31 | + }) |
| 32 | + |
| 33 | + let justBack = false |
| 34 | + useTransitionLeave(({ from, to }, complete) => { |
| 35 | + /// 如果该值 设置为 true 表明,当前是通过 滑动返回的方式关闭的 |
| 36 | + /// 那么不需要执行任何动画 直接完成即可 |
| 37 | + if (justBack) { |
| 38 | + complete() |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + const an = anime.timeline({ ...animeProps, complete }) |
| 43 | + if (to?.classList.contains(PageBodyClassName)) { |
| 44 | + an.add({ targets: to, translateX: ['-50%', '0%'] }) |
| 45 | + } |
| 46 | + an.add({ targets: from, translateX: ['0', '100%'] }, 0) |
| 47 | + }) |
| 48 | + |
| 49 | + useProgressExitAnimated(({ from, to }, progress, isFinish) => { |
| 50 | + if (isFinish === undefined) { |
| 51 | + if (to?.classList.contains(PageBodyClassName)) { |
| 52 | + anime.set(to, { translateX: -50 * (1 - progress) + '%' }) |
| 53 | + } |
| 54 | + if (from) { |
| 55 | + anime.set(from, { translateX: 100 * progress + '%' }) |
| 56 | + } |
| 57 | + } else { |
| 58 | + /// 如果 滑动返回成功结束 |
| 59 | + if (isFinish) { |
| 60 | + const animated = anime.timeline({ |
| 61 | + duration: 300, |
| 62 | + easing: 'easeOutExpo', |
| 63 | + }) |
| 64 | + if (to?.classList.contains(PageBodyClassName)) { |
| 65 | + animated.add({ targets: to, translateX: '0' }) |
| 66 | + } |
| 67 | + animated.add({ targets: from, translateX: '100%' }, 0) |
| 68 | + |
| 69 | + animated.complete = () => { |
| 70 | + justBack = true |
| 71 | + back() |
| 72 | + } |
| 73 | + } else { |
| 74 | + const animated = anime.timeline({ |
| 75 | + duration: 300, |
| 76 | + easing: 'easeOutExpo', |
| 77 | + }) |
| 78 | + if (to?.classList.contains(PageBodyClassName)) { |
| 79 | + animated.add({ targets: to, translateX: '-50%' }) |
| 80 | + } |
| 81 | + animated.add({ targets: from, translateX: '0%' }, 0) |
| 82 | + } |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + return () => <Page>{slots.default?.()}</Page> |
| 87 | + }, |
| 88 | +}) |
| 89 | + |
| 90 | +export default NavPage |
0 commit comments