Skip to content

Commit acc39e2

Browse files
authored
Merge pull request #1 from TeaEntityLab/johnteee-patch-1
Add panelmode & make width auto, min-width 300px.
2 parents 86afcae + ef7e535 commit acc39e2

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

Drawer.vue

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<section v-if="enabled">
2+
<section v-if="enabled" ref="rootEl">
33
<aside class="sidebar" :style="style" ref="element">
44
<slot></slot>
55
</aside>
@@ -8,7 +8,7 @@
88
</template>
99
<script>
1010
export default{
11-
props:['direction','exist'],
11+
props:['direction','exist','panelmode'],
1212
data(){
1313
return {
1414
auto_speed:'0.3s',
@@ -47,7 +47,7 @@
4747
document.addEventListener('touchend',(e) =>{this.handleEnd(e)});
4848
document.addEventListener('touchcancel',(e) =>{this.handleEnd(e)});
4949
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);
5151
this.overlay.addEventListener('click',(e) =>{this.close()},false);
5252
this.setVisibality();
5353
},
@@ -62,7 +62,7 @@
6262
handleStart(e){
6363
this.startTime = new Date().getTime();
6464
this.startPos = e.targetTouches[0].pageX;
65-
this.element.style.transitionDuration = this.manual_speed;
65+
this.element.style.transitionDuration = this.manual_speed;
6666
},
6767
handleMove(e){
6868
let gesture = this.gesture(e);
@@ -74,14 +74,14 @@
7474
if(this.translate < 0){
7575
this.element.style.transform = 'translate3d('+ this.translate +'px,0,0)';
7676
}else{
77-
this.open();
77+
this.open();
7878
}
7979
}else{
8080
this.translate = -(screen.width-this.element.offsetWidth-e.touches[0].pageX);
8181
if(this.translate > 0){
8282
this.element.style.transform = 'translate3d('+ this.translate +'px,0,0)';
8383
}else{
84-
this.open();
84+
this.open();
8585
}
8686
}
8787
this.overlayOpacity(percent/100);
@@ -91,29 +91,26 @@
9191
let speed = this.speed(e);
9292
let gesture = this.gesture(e);
9393
let valid = this.validate(this.direction,gesture);
94-
9594
if(valid){
9695
if(speed>0.6){
9796
if(!this.active){
98-
this.open();
97+
this.open();
9998
}else{
100-
this.close();
99+
this.close();
101100
}
102101
}else{
103102
if(this.element.offsetWidth/2>Math.abs(this.translate)){
104-
this.open();
103+
this.open();
105104
}else{
106-
this.close();
107-
105+
this.close();
108106
}
109-
}
107+
}
110108
}
111-
112109
},
113110
handleZindex(){
114111
let opacity = window.getComputedStyle(this.overlay).getPropertyValue('opacity');
115112
if(opacity<=0){
116-
this.overlay.style.zIndex = -999;
113+
this.overlay.style.zIndex = -999;
117114
}
118115
},
119116
validate(direction,gesture){
@@ -126,15 +123,15 @@
126123
return false;
127124
}
128125
}
129-
if((document.querySelector('.sidebar.active') && !this.active) || !this.visible){
126+
if((this.$refs.rootEl.querySelector('.sidebar.active') && !this.active) || !this.visible){
130127
return false;
131128
}
132129
return true;
133130
},
134131
overlayOpacity(opacity){
135132
this.overlay.style.opacity = opacity;
136133
if(opacity > 0){
137-
this.overlay.style.zIndex = 999;
134+
this.overlay.style.zIndex = 999;
138135
}
139136
},
140137
gesture(e){
@@ -148,33 +145,37 @@
148145
}else{
149146
return directions[0];
150147
}
151-
152148
},
153-
154149
open(){
155150
this.translate = 0;
156151
this.element.style.transform = 'translate3d('+this.translate+',0,0)';
157152
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');
163162
},
164163
close(){
165164
if(this.direction=='left'){
166165
this.translate = '-'+this.element.offsetWidth+'px';
167166
}else{
168167
this.translate = this.element.offsetWidth+'px';
169168
}
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);
173174
this.unlock(document.querySelector('html'));
174175
this.unlock(document.querySelector('body'));
175-
this.element.classList.remove('active');
176+
}
177+
this.element.classList.remove('active');
176178
this.active = false;
177-
178179
},
179180
speed(e){
180181
let time = new Date().getTime() - this.startTime;
@@ -189,7 +190,7 @@
189190
if(direction == 'left'){
190191
test = ['swipeleft','swiperight']
191192
}else{
192-
test = ['swiperight','swipeleft']
193+
test = ['swiperight','swipeleft']
193194
}
194195
if(this.active && gesture == test[0]){
195196
percentage = 100-Math.round(Math.abs(this.translate)/this.element.offsetWidth*100);
@@ -212,9 +213,9 @@
212213
unlock(element){
213214
element.style.removeProperty('overflow');
214215
element.style.removeProperty('touch-action');
215-
}
216+
}
216217
}
217-
}
218+
}
218219
</script>
219220
<style scoped>
220221
div.overlay{
@@ -234,11 +235,12 @@
234235
will-change: transform;
235236
height: 100%;
236237
top: 0px;
237-
transition:transform 0.3s ease;
238+
transition:transform 0.3s ease;
238239
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;
243245
}
244-
</style>
246+
</style>

0 commit comments

Comments
 (0)