1
1
<template >
2
- <section v-if =" enabled" >
2
+ <section v-if =" enabled" ref = " rootEl " >
3
3
<aside class =" sidebar" :style =" style" ref =" element" >
4
4
<slot ></slot >
5
5
</aside >
8
8
</template >
9
9
<script >
10
10
export default {
11
- props: [' direction' ,' exist' ],
11
+ props: [' direction' ,' exist' , ' panelmode ' ],
12
12
data (){
13
13
return {
14
14
auto_speed: ' 0.3s' ,
47
47
document .addEventListener (' touchend' ,(e ) => {this .handleEnd (e)});
48
48
document .addEventListener (' touchcancel' ,(e ) => {this .handleEnd (e)});
49
49
window .addEventListener (' resize' , (e ) => {this .setVisibality (e)}, true );
50
- this .overlay .addEventListener (' transitionend' ,(e ) => {this .handleZindex (e)},false );
50
+ this .overlay .addEventListener (' transitionend' ,(e ) => {this .handleZindex (e)},false );
51
51
this .overlay .addEventListener (' click' ,(e ) => {this .close ()},false );
52
52
this .setVisibality ();
53
53
},
62
62
handleStart (e ){
63
63
this .startTime = new Date ().getTime ();
64
64
this .startPos = e .targetTouches [0 ].pageX ;
65
- this .element .style .transitionDuration = this .manual_speed ;
65
+ this .element .style .transitionDuration = this .manual_speed ;
66
66
},
67
67
handleMove (e ){
68
68
let gesture = this .gesture (e);
74
74
if (this .translate < 0 ){
75
75
this .element .style .transform = ' translate3d(' + this .translate + ' px,0,0)' ;
76
76
}else {
77
- this .open ();
77
+ this .open ();
78
78
}
79
79
}else {
80
80
this .translate = - (screen .width - this .element .offsetWidth - e .touches [0 ].pageX );
81
81
if (this .translate > 0 ){
82
82
this .element .style .transform = ' translate3d(' + this .translate + ' px,0,0)' ;
83
83
}else {
84
- this .open ();
84
+ this .open ();
85
85
}
86
86
}
87
87
this .overlayOpacity (percent/ 100 );
91
91
let speed = this .speed (e);
92
92
let gesture = this .gesture (e);
93
93
let valid = this .validate (this .direction ,gesture);
94
-
95
94
if (valid){
96
95
if (speed> 0.6 ){
97
96
if (! this .active ){
98
- this .open ();
97
+ this .open ();
99
98
}else {
100
- this .close ();
99
+ this .close ();
101
100
}
102
101
}else {
103
102
if (this .element .offsetWidth / 2 > Math .abs (this .translate )){
104
- this .open ();
103
+ this .open ();
105
104
}else {
106
- this .close ();
107
-
105
+ this .close ();
108
106
}
109
- }
107
+ }
110
108
}
111
-
112
109
},
113
110
handleZindex (){
114
111
let opacity = window .getComputedStyle (this .overlay ).getPropertyValue (' opacity' );
115
112
if (opacity<= 0 ){
116
- this .overlay .style .zIndex = - 999 ;
113
+ this .overlay .style .zIndex = - 999 ;
117
114
}
118
115
},
119
116
validate (direction ,gesture ){
126
123
return false ;
127
124
}
128
125
}
129
- if ((document .querySelector (' .sidebar.active' ) && ! this .active ) || ! this .visible ){
126
+ if ((this . $refs . rootEl .querySelector (' .sidebar.active' ) && ! this .active ) || ! this .visible ){
130
127
return false ;
131
128
}
132
129
return true ;
133
130
},
134
131
overlayOpacity (opacity ){
135
132
this .overlay .style .opacity = opacity;
136
133
if (opacity > 0 ){
137
- this .overlay .style .zIndex = 999 ;
134
+ this .overlay .style .zIndex = 999 ;
138
135
}
139
136
},
140
137
gesture (e ){
148
145
}else {
149
146
return directions[0 ];
150
147
}
151
-
152
148
},
153
-
154
149
open (){
155
150
this .translate = 0 ;
156
151
this .element .style .transform = ' translate3d(' + this .translate + ' ,0,0)' ;
157
152
this .element .style .transitionDuration = this .auto_speed ;
158
- this .overlayOpacity (1 );
159
- this .lock (document .querySelector (' html' ));
160
- this .lock (document .querySelector (' body' ));
161
- this .element .classList .add (' active' );
162
- this .active = true ;
153
+
154
+ if (this .panelmode === undefined ) {
155
+ this .overlayOpacity (1 );
156
+ this .lock (document .querySelector (' html' ));
157
+ this .lock (document .querySelector (' body' ));
158
+ }
159
+ this .active = true ;
160
+
161
+ this .element .classList .add (' active' );
163
162
},
164
163
close (){
165
164
if (this .direction == ' left' ){
166
165
this .translate = ' -' + this .element .offsetWidth + ' px' ;
167
166
}else {
168
167
this .translate = this .element .offsetWidth + ' px' ;
169
168
}
170
- this .element .style .transform = ' translate3d(' + this .translate + ' ,0,0)' ;
171
- this .element .style .transitionDuration = this .auto_speed ;
172
- this .overlayOpacity (0 );
169
+ this .element .style .transform = ' translate3d(' + this .translate + ' ,0,0)' ;
170
+ this .element .style .transitionDuration = this .auto_speed ;
171
+
172
+ if (this .panelmode === undefined ) {
173
+ this .overlayOpacity (0 );
173
174
this .unlock (document .querySelector (' html' ));
174
175
this .unlock (document .querySelector (' body' ));
175
- this .element .classList .remove (' active' );
176
+ }
177
+ this .element .classList .remove (' active' );
176
178
this .active = false ;
177
-
178
179
},
179
180
speed (e ){
180
181
let time = new Date ().getTime () - this .startTime ;
189
190
if (direction == ' left' ){
190
191
test = [' swipeleft' ,' swiperight' ]
191
192
}else {
192
- test = [' swiperight' ,' swipeleft' ]
193
+ test = [' swiperight' ,' swipeleft' ]
193
194
}
194
195
if (this .active && gesture == test[0 ]){
195
196
percentage = 100 - Math .round (Math .abs (this .translate )/ this .element .offsetWidth * 100 );
212
213
unlock (element ){
213
214
element .style .removeProperty (' overflow' );
214
215
element .style .removeProperty (' touch-action' );
215
- }
216
+ }
216
217
}
217
- }
218
+ }
218
219
</script >
219
220
<style scoped>
220
221
div .overlay {
234
235
will-change : transform;
235
236
height : 100% ;
236
237
top : 0px ;
237
- transition :transform 0.3s ease ;
238
+ transition :transform 0.3s ease ;
238
239
background :#fff ;
239
- width : 300px ;
240
- overflow-y : auto ;
241
- overflow-x : hidden ;
242
- word-wrap : break-word ;
240
+ width : auto ;
241
+ min-width : 300px ;
242
+ overflow-y : auto ;
243
+ overflow-x : hidden ;
244
+ word-wrap : break-word ;
243
245
}
244
- </style >
246
+ </style >
0 commit comments