// 動畫效果可以在此更改為需要的
const elTransition = 
'0.4s height ease-in-out, 0.4s padding-top ease-in-out, 0.4s padding-bottom ease-in-out';

const Transition = {
    'before-enter'(el) {
        el.style.transition = elTransition;
        if (!el.dataset) el.dataset = {};

        el.dataset.oldPaddingTop = el.style.paddingTop;
        el.dataset.oldPaddingBottom = el.style.paddingBottom;

        el.style.height = 0;
        el.style.paddingTop = 0;
        el.style.paddingBottom = 0;
    },

    enter(el) {
        el.dataset.oldOverflow = el.style.overflow;
        if (el.scrollHeight !== 0) {
            el.style.height = el.scrollHeight + 'px';
            el.style.paddingTop = el.dataset.oldPaddingTop;
            el.style.paddingBottom = el.dataset.oldPaddingBottom;
        } else {
            el.style.height = '';
            el.style.paddingTop = el.dataset.oldPaddingTop;
            el.style.paddingBottom = el.dataset.oldPaddingBottom;
        }

        el.style.overflow = 'hidden';
    },

    'after-enter'(el) {
        el.style.transition = '';
        el.style.height = '';
        el.style.overflow = el.dataset.oldOverflow;
    },

    'before-leave'(el) {
        if (!el.dataset) el.dataset = {};
        el.dataset.oldPaddingTop = el.style.paddingTop;
        el.dataset.oldPaddingBottom = el.style.paddingBottom;
        el.dataset.oldOverflow = el.style.overflow;

        el.style.height = el.scrollHeight + 'px';
        el.style.overflow = 'hidden';
    },

    leave(el) {
        if (el.scrollHeight !== 0) {
            el.style.transition = elTransition;
            el.style.height = 0;
            el.style.paddingTop = 0;
            el.style.paddingBottom = 0;
        }
    },

    'after-leave'(el) {
        el.style.transition = '';
        el.style.height = '';
        el.style.overflow = el.dataset.oldOverflow;
        el.style.paddingTop = el.dataset.oldPaddingTop;
        el.style.paddingBottom = el.dataset.oldPaddingBottom;
    },
};

export default {
    name: 'collapseTransition',
    functional: true,
    render(h, { children }) {
        const data = {
            on: Transition,
        };
        return h('transition', data, children);
    },
};


<template>
    <div>
        <!-- html use -->
        <collapse>
            <ul>
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
                <li>11</li>
                <li>22</li>
                <li>33</li>
                <li>44</li>
                <li>55</li>
            </ul>
        </collapse>
    </div>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';

// use
import collapse from './collapse';

export default class VuePageClass extends Vue {}
</script>

<style lang="scss" scoped>
</style>

    文章標籤

    HTML vue 收合動畫

    全站熱搜

    bingzhichen 發表在 痞客邦 留言(0) 人氣()