PageToggleTransition.vue 2.63 KB
Newer Older
wb-ct393452's avatar
wb-ct393452 committed
1
<template>
2 3 4 5 6 7 8 9
    <transition
        v-if="!disabled"
        :enter-active-class="`animated ${enterAnimate} page-toggle-enter-active`"
        :leave-active-class="`animated ${leaveAnimate} page-toggle-leave-active`"
    >
        <slot></slot>
    </transition>
    <div v-else><slot></slot></div>
wb-ct393452's avatar
wb-ct393452 committed
10 11 12
</template>

<script>
13
import { globalConfig } from '@/config';
wb-ct393452's avatar
wb-ct393452 committed
14

15
const animates = globalConfig.animates.preset;
wb-ct393452's avatar
wb-ct393452 committed
16

17
export default {
wb-ct393452's avatar
wb-ct393452 committed
18 19
    name: 'PageToggleTransition',
    props: {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
        disabled: {
            type: Boolean,
            default: false,
        },
        animate: {
            type: String,
            validator(value) {
                return animates.findIndex(item => item.name == value) != -1;
            },
            default: 'bounce',
        },
        direction: {
            type: String,
        },
        reverse: {
            type: Boolean,
            default: true,
wb-ct393452's avatar
wb-ct393452 committed
37 38 39
        },
    },
    computed: {
40 41 42 43 44 45
        enterAnimate() {
            return this.activeClass(false);
        },
        leaveAnimate() {
            return this.activeClass(true);
        },
wb-ct393452's avatar
wb-ct393452 committed
46 47
    },
    methods: {
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        activeClass(isLeave) {
            let animate = animates.find(item => this.animate == item.name);
            if (animate == undefined) {
                return '';
            }
            let direction = '';
            if (this.direction == undefined) {
                direction = animate.directions[0];
            } else {
                direction = animate.directions.find(item => item == this.direction);
            }
            direction = direction == undefined || direction === 'default' ? '' : direction;
            if (direction != '') {
                direction =
                    isLeave && this.reverse ? this.reversePosition(direction, animate.directions) : direction;
                direction = direction[0].toUpperCase() + direction.substring(1);
            }
            let t = isLeave ? 'Out' : 'In';
            return animate.name + t + direction;
        },
        reversePosition(direction, directions) {
            if (direction.length == 0 || direction == 'x' || direction == 'y') {
                return direction;
            }
            let index = directions.indexOf(direction);
            index = index % 2 == 1 ? index - 1 : index + 1;
            return directions[index];
        },
    },
};
wb-ct393452's avatar
wb-ct393452 committed
78 79 80
</script>

<style lang="less">
81
.page-toggle-enter-active {
wb-ct393452's avatar
wb-ct393452 committed
82 83 84
    position: absolute !important;
    animation-duration: 0.8s !important;
    width: calc(100%) !important;
85 86
}
.page-toggle-leave-active {
wb-ct393452's avatar
wb-ct393452 committed
87 88 89
    position: absolute !important;
    animation-duration: 0.8s !important;
    width: calc(100%) !important;
90
}
wb-ct393452's avatar
wb-ct393452 committed
91
</style>