-
Notifications
You must be signed in to change notification settings - Fork 80
/
vue-scrollbar.vue
322 lines (250 loc) · 8.64 KB
/
vue-scrollbar.vue
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<template>
<div
@click="calculateSize"
:class="'vue-scrollbar__wrapper' + ( this.classes ? ' ' + this.classes : '' )"
ref="scrollWrapper"
:style="this.styles">
<div
:class="'vue-scrollbar__area' + ( this.dragging ? ' ' : ' vue-scrollbar-transition')"
ref="scrollArea"
@wheel="scroll"
@touchstart="startDrag"
@touchmove="onDrag"
@touchend="stopDrag"
:style="{
marginTop: this.top * -1 +'px',
marginLeft: this.left * -1 +'px'
}">
<slot></slot>
<vertical-scrollbar
v-if="ready"
:area="{ height: scrollAreaHeight }"
:wrapper="{ height: scrollWrapperHeight }"
:scrolling="vMovement"
:dragging-from-parent="dragging"
:on-change-position="handleChangePosition"
:on-dragging="handleScrollbarDragging"
:on-stop-drag="handleScrollbarStopDrag" >
</vertical-scrollbar>
<horizontal-scrollbar
v-if="ready"
:area="{ width: scrollAreaWidth }"
:wrapper="{ width: scrollWrapperWidth }"
:scrolling="hMovement"
:dragging-from-parent="dragging"
:on-change-position="handleChangePosition"
:on-dragging="handleScrollbarDragging"
:on-stop-drag="handleScrollbarStopDrag">
</horizontal-scrollbar>
</div>
</div>
</template>
<script>
import VerticalScrollbar from './vertical-scrollbar.vue';
import HorizontalScrollbar from './horizontal-scrollbar.vue';
export default {
props: {
classes: String,
styles: Object,
speed: {
type: Number,
default: 53
},
onMaxScroll: Function,
},
components: {
VerticalScrollbar,
HorizontalScrollbar
},
data () {
return {
ready: false,
top: 0,
left: 0,
scrollAreaHeight: null,
scrollAreaWidth: null,
scrollWrapperHeight: null,
scrollWrapperWidth: null,
vMovement: 0,
hMovement: 0,
dragging: false,
start: { y: 0, x: 0},
allowBodyScroll: false,
}
},
methods: {
scroll(e){
// Make sure the content height is not changed
this.calculateSize(() => {
// Set the wheel step
let num = this.speed
// DOM events
let shifted = e.shiftKey
let scrollY = e.deltaY > 0 ? num : -(num)
let scrollX = e.deltaX > 0 ? num : -(num)
// Fix Mozilla Shifted Wheel~
if(shifted && e.deltaX == 0) scrollX = e.deltaY > 0 ? num : -(num)
// Next Value
let nextY = this.top + scrollY
let nextX = this.left + scrollX
// Is it Scrollable?
let canScrollY = this.scrollAreaHeight > this.scrollWrapperHeight
let canScrollX = this.scrollAreaWidth > this.scrollWrapperWidth
// Vertical Scrolling
if(canScrollY && !shifted) this.normalizeVertical(nextY)
// Horizontal Scrolling
if(shifted && canScrollX) this.normalizeHorizontal(nextX)
})
// prevent Default only if scrolled content is not at the top/bottom
if (!this.allowBodyScroll) {
e.preventDefault()
e.stopPropagation()
}
},
// DRAG EVENT JUST FOR TOUCH DEVICE~
startDrag(e){
this.touchEvent = e
const evt = e.changedTouches ? e.changedTouches[0] : e
// Make sure the content height is not changed
this.calculateSize(() => {
// Prepare to drag
this.dragging = true,
this.start = { y: evt.pageY, x: evt.pageX }
})
},
onDrag(e){
if(this.dragging){
e.preventDefault()
e.stopPropagation()
// Prevent Click Event When it dragging
if (this.touchEvent) {
this.touchEvent.preventDefault()
this.touchEvent.stopPropagation()
}
let evt = e.changedTouches ? e.changedTouches[0] : e
// Invers the Movement
let yMovement = this.start.y - evt.clientY
let xMovement = this.start.x - evt.clientX
// Update the last e.client
this.start = { y: evt.clientY, x: evt.clientX }
// The next Vertical Value will be
let nextY = this.top + yMovement
let nextX = this.left + xMovement
this.normalizeVertical(nextY)
this.normalizeHorizontal(nextX)
}
},
stopDrag(e){
this.dragging = false
this.touchEvent = false
},
scrollToY(y) {
this.normalizeVertical(y)
},
scrollToX(x) {
this.normalizeHorizontal(x)
},
normalizeVertical(next){
const elementSize = this.getSize()
// Vertical Scrolling
const lowerEnd = elementSize.scrollAreaHeight - elementSize.scrollWrapperHeight
// Max Scroll Down
const maxBottom = next > lowerEnd
if(maxBottom) next = lowerEnd
// Max Scroll Up
const maxTop = next < 0
if(maxTop) next = 0
// Update the Vertical Value if it's needed
const shouldScroll = this.top !== next
this.allowBodyScroll = !shouldScroll
if (shouldScroll) {
this.top = next,
this.vMovement = next / elementSize.scrollAreaHeight * 100
if (this.onMaxScroll && (maxTop || maxBottom)) {
this.onMaxScroll({ top: maxTop, bottom: maxBottom, right: false, left: false })
}
}
},
normalizeHorizontal(next){
const elementSize = this.getSize()
// Horizontal Scrolling
const rightEnd = elementSize.scrollAreaWidth - this.scrollWrapperWidth
// Max Scroll Right
const maxRight = next > rightEnd
if(maxRight) next = rightEnd;
// Max Scroll Left
const maxLeft = next < 0
if(next < 0) next = 0
// Update the Horizontal Value
const shouldScroll = this.left !== next
this.allowBodyScroll = !shouldScroll
if (shouldScroll) {
this.left = next,
this.hMovement = next / elementSize.scrollAreaWidth * 100
if (this.onMaxScroll && (maxRight || maxLeft)) {
this.onMaxScroll({ right: maxRight, left: maxLeft, top: false, bottom: false })
}
}
},
handleChangePosition(movement, orientation){
// Make sure the content height is not changed
this.calculateSize(() => {
// Convert Percentage to Pixel
let next = movement / 100
if( orientation == 'vertical' ) this.normalizeVertical( next * this.scrollAreaHeight )
if( orientation == 'horizontal' ) this.normalizeHorizontal( next * this.scrollAreaWidth )
})
},
handleScrollbarDragging(){
this.dragging = true
},
handleScrollbarStopDrag(){
this.dragging = false
},
getSize(){
// The Elements
let $scrollArea = this.$refs.scrollArea
let $scrollWrapper = this.$refs.scrollWrapper
// Get new Elements Size
let elementSize = {
// Scroll Area Height and Width
scrollAreaHeight: $scrollArea.children[0].clientHeight,
scrollAreaWidth: $scrollArea.children[0].clientWidth,
// Scroll Wrapper Height and Width
scrollWrapperHeight: $scrollWrapper.clientHeight,
scrollWrapperWidth: $scrollWrapper.clientWidth,
}
return elementSize
},
calculateSize(cb){
if(typeof cb !== 'function') cb = null;
let elementSize = this.getSize()
if( elementSize.scrollWrapperHeight !== this.scrollWrapperHeight ||
elementSize.scrollWrapperWidth !== this.scrollWrapperWidth ||
elementSize.scrollAreaHeight !== this.scrollAreaHeight ||
elementSize.scrollAreaWidth !== this.scrollAreaWidth )
{
// Scroll Area Height and Width
this.scrollAreaHeight = elementSize.scrollAreaHeight,
this.scrollAreaWidth = elementSize.scrollAreaWidth,
// Scroll Wrapper Height and Width
this.scrollWrapperHeight = elementSize.scrollWrapperHeight,
this.scrollWrapperWidth = elementSize.scrollWrapperWidth,
// Make sure The wrapper is Ready, then render the scrollbar
this.ready = true
return cb ? cb() : false
}
else return cb ? cb() : false
}
},
mounted () {
this.calculateSize()
// Attach The Event for Responsive View~
window.addEventListener('resize', this.calculateSize)
},
beforeDestroy (){
// Remove Event
window.removeEventListener('resize', this.calculateSize)
}
}
</script>