@@ -4,25 +4,42 @@ const props = defineProps({
4
4
show: Boolean , // 是否显示 Drawer
5
5
direction: { type: String , default: ' bottom' , values: [' top' , ' bottom' , ' left' , ' right' ] },
6
6
distance: { type: [String , Number ], default: ' 100%' },
7
+ withHeader: { type: Boolean , default: true },
7
8
})
8
9
10
+ const emit = defineEmits ([' close' ])
11
+
9
12
const isHorizontal = computed (() => props .direction === ' left' || props .direction === ' right' )
13
+
14
+ function handleMaskClick() {
15
+ emit (' close' )
16
+ }
17
+
18
+ var move = 0
19
+ function handeContainerTouchStart(e : TouchEvent ) {
20
+ move = e .changedTouches [0 ].pageY
21
+ }
22
+
23
+ function handeContainerTouchMove(e : TouchEvent ) {
24
+ let moveEndY = e .changedTouches [0 ].pageY
25
+ let Y = moveEndY - move // 如果值为正,则代表手指下滑,反则则为上滑,为0则表示点击
26
+ if (Y > 0 ) {
27
+ emit (' close' )
28
+ }
29
+ }
10
30
</script >
11
31
12
32
<template >
13
33
<Transition name =" drawer" >
14
- <div v-if =" show" class =" drawer-mask " >
34
+ <div v-if =" show" class =" drawer-layout " >
15
35
<div
16
36
class =" drawer-container"
17
- :style ="
18
- isHorizontal
19
- ? 'width: ' + distance + '; height: 100%;'
20
- : 'height: ' + distance + '; width: 100%;'
21
- "
37
+ @touchstart =" handeContainerTouchStart"
38
+ @touchmove =" handeContainerTouchMove"
22
39
>
23
- <div class =" drawer-header" >
40
+ <header v-if = " withHeader " class =" drawer-header" >
24
41
<slot name =" header" />
25
- </div >
42
+ </header >
26
43
27
44
<div class =" drawer-body" >
28
45
<slot name =" body" />
@@ -32,12 +49,13 @@ const isHorizontal = computed(() => props.direction === 'left' || props.directio
32
49
<slot name =" footer" />
33
50
</div >
34
51
</div >
52
+ <div class =" drawer-mask" @click =" handleMaskClick" ></div >
35
53
</div >
36
54
</Transition >
37
55
</template >
38
56
39
57
<style >
40
- .drawer-mask {
58
+ .drawer-layout {
41
59
position : fixed ;
42
60
z-index : 9998 ;
43
61
top : 0 ;
@@ -46,15 +64,30 @@ const isHorizontal = computed(() => props.direction === 'left' || props.directio
46
64
bottom : 0 ;
47
65
width : 100% ;
48
66
height : 100% ;
49
- background-color : rgba ( 0 , 0 , 0 , 0.5 ) ;
67
+ background-color : inherit ;
50
68
display : flex ;
51
69
transition : opacity 0.3s ease ;
52
70
}
71
+ .drawer-mask {
72
+ position : fixed ;
73
+ z-index : 818 ;
74
+ top : 0 ;
75
+ left : 0 ;
76
+ right : 0 ;
77
+ bottom : 0 ;
78
+ width : 100% ;
79
+ height : 100% ;
80
+ background-color : rgba (0 , 0 , 0 , 0.5 );
81
+ transition : opacity 0.3s ease ;
82
+ }
53
83
54
84
.drawer-container {
85
+ z-index : 9998 ;
55
86
overflow : auto ;
56
87
position : absolute ;
57
88
bottom : 0 ;
89
+ width : 100% ;
90
+ height : 100% ;
58
91
background-color : #f5f5f5 ;
59
92
box-shadow : 0 2px 8px rgba (0 , 0 , 0 , 0.33 );
60
93
transition : all 0.3s ease ;
@@ -80,4 +113,18 @@ const isHorizontal = computed(() => props.direction === 'left' || props.directio
80
113
.drawer-leave-to .drawer-mask {
81
114
opacity : 0 ;
82
115
}
116
+
117
+ @media (min-width : 1024px ) {
118
+ .drawer-container {
119
+ bottom : unset ;
120
+ right : 0 ;
121
+ width : 37% ;
122
+ height : 100% ;
123
+ }
124
+
125
+ .drawer-enter-from .drawer-container ,
126
+ .drawer-leave-to .drawer-container {
127
+ transform : translate (100% , 0 );
128
+ }
129
+ }
83
130
</style >
0 commit comments