transition折叠效果,重写transition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const elTransition = '0.3s all ease';
const Transition = {
beforeEnter(el) {
el.style.transition = elTransition;
if (!el.dataset) el.dataset = {};

el.style.height = 0;
el.style.opacity = 0;
},

enter(el) {
if (el.scrollHeight !== 0) {
el.style.height = `${el.scrollHeight}px`;
el.style.opacity = 1
} else {
el.style.height = '';
el.style.opacity = ''
}
el.style.overflow = 'hidden';
},

afterEnter(el) {
el.style.transition = '';
el.style.height = '';
el.style.opacity = ''
},

beforeLeave(el) {
if (!el.dataset) el.dataset = {};
el.style.display = 'block';
el.style.height = `${el.scrollHeight}px`;
el.style.overflow = 'hidden';
el.style.opacity = 1
},

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

afterLeave(el) {
el.style.transition = '';
el.style.height = '';
el.style.opacity = ''
},
};

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

应用:左边导航栏树型组件动画

1
2
3
4
5
6
7
8
9
10
<template>
<div class="level-1">
<div class="label"></div>
<collapse-transition>
<div class="level-2-menu" v-show="levelOne.expand">
...
</div>
</collapse-transition>
</div>
</template>