-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollbar.js
528 lines (471 loc) · 15.5 KB
/
scrollbar.js
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*!
* scrollbar v1.1.0 by Ivan Yan 2014-01-24
* https://github.com/yanxyz/jquery.scrollbar
* MIT License
*/
;(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory)
} else {
factory(jQuery);
}
}(function($) {
'use strict';
function Scrollbar(element, options) {
this.$elem = $(element) // 滚动条所属容器
this.options = options
if (options.scroller) { // 容器不是滚动元素,即两层以上结构
this.type = 1
this.$scroller = this.$elem.find(options.scroller).eq(0)
} else { // 容器是滚动元素, 即两层结构
this.type = 0
this.$scroller = this.$elem
}
this.namespace = '.scrollbar' // 事件命名空间
this.hasX = false // 是否启用了滚动条
this.hasY = false
}
Scrollbar.defaults = {
// scroller: '', // 滚动元素 selector
axis: 'y', // 默认只有垂直滚动条,其它值:'x', 'xy'
minThumb: 20, // 滑块最小高/宽
speed: 40, // 每次滚动的像素数
wheelPropagation: false, // 滚动到头后是否冒泡,这样可接着滚动外层的滚动条
wheelBoth: false, // 只有一个滚动条时两个滚轴是否均可控制
keyboard: true // 是否支持键盘方向键滚动
}
$.extend(Scrollbar.prototype, {
_init: function() {
var options = this.options
var $elem = this.$elem
var that = this
// 先检查是否已有 scrollbar
function createBar(axis) {
var $bar
switch(axis) {
case 'x':
$bar = $elem.children('.scrollbar-x')
if (!$bar.length) {
$bar = $('<div class="scrollbar scrollbar-x" style="display: none;"><div class="scrollbar-thumb"></div></div>').appendTo($elem)
}
that.hasX = true
that.$barX = $bar
that.$thumbX = $bar.children('.scrollbar-thumb')
break
case 'y':
$bar = $elem.children('.scrollbar-y')
if (!$bar.length) {
$bar = $('<div class="scrollbar scrollbar-y" style="display: none;"><div class="scrollbar-thumb"></div></div>').appendTo($elem)
}
that.hasY = true
that.$barY = $bar
that.$thumbY = $bar.children('.scrollbar-thumb')
break
}
}
switch (options.axis) {
case 'x':
createBar('x')
break
case 'y':
createBar('y')
break
case 'xy':
createBar('x')
createBar('y')
}
this.refresh()
this._bind()
},
refresh: function() {
var $elem = this.$scroller
var options = this.options
// TODO: 是否考虑 padding 呢
var w = this.containerWidth = $elem.width()
var h = this.containerHeight = $elem.height()
var W = this.contentWidth = $elem.prop('scrollWidth')
var H = this.contentHeight = $elem.prop('scrollHeight')
var deltaX = this.maxX = W - w
var deltaY = this.maxY = H - h
var left = $elem.scrollLeft()
var top = $elem.scrollTop()
var w1, h1, b, r
if (this.hasX && deltaX > 0) {
this.barXActive = true
b = this.barXBottom = parseInt(this.$barX.css('bottom'), 10)
// 滑块宽度 x/w = w/W
w1 = this.thumbXWidth = Math.max(options.minThumb, parseInt(w * w / W, 10))
// 后面计算滑块的 left 值: elemleft*ratio
this.ratioX = (w - w1) / deltaX
if (this.type) {
this.$barX.css({
display: 'block',
width: w
})
} else {
this.$barX.css({
width: w,
left: left,
bottom: b - top
})
}
this.$thumbX.width(w1)
} else {
this.hasX && this.$barX.hide()
this.barXActive = false
}
if (this.hasY && deltaY > 0) {
this.barYActive = true
r = this.barYRight = parseInt(this.$barY.css('right'), 10)
h1 = this.thumbYHeight = Math.max(options.minThumb, parseInt(h * h / H, 10))
this.ratioY = (h - h1) / deltaY
if (this.type) {
this.$barY.css({
display: 'block',
height: h
})
} else {
this.$barY.css({
height: h,
top: top,
right: r - left
})
}
this.$thumbY.height(h1)
} else {
this.hasY && this.$barY.hide()
this.barYActive = false
}
this.update()
},
update: function() {
var left = this.$scroller.scrollLeft()
var top = this.$scroller.scrollTop()
// TODO: scrollTop 一直增加,只好这里处理
left = left > this.maxX ? this.maxX : left
top = top > this.maxY ? this.maxY : top
if (this.barXActive) {
if (!this.type) {
this.$barX.css({
left: left,
bottom: this.barXBottom - top
}).show()
}
this.$thumbX.css('left', parseInt(left * this.ratioX, 10))
}
if (this.barYActive) {
if (!this.type) {
this.$barY.css({
top: top,
right: this.barYRight - left
}).show()
}
this.$thumbY.css('top', parseInt(top * this.ratioY, 10))
}
},
_bind: function() {
var that = this
var options = this.options
var speed = this.options.speed
var ns = this.namespace
var $elem = this.$scroller
var $barX = this.$barX
var $thumbX = this.$thumbX
var $barY = this.$barY
var $thumbY = this.$thumbY
var $doc = $(document)
var hasX = this.hasX
var hasY = this.hasY
var x = this.barXActive
var y = this.barYActive
// 两个滚轴各管各的,忽略 wheelBoth
// 默认不冒泡
function isPropagated(deltaX, deltaY) {
var left, top
// 确定冒泡再检查
if (options.wheelPropagation) {
if (x && deltaX !== 0) {
left = $elem.scrollLeft()
if ((left === 0 && deltaX < 0) ||
(left >= that.maxX && deltaX > 0)) {
return options.wheelPropagation
}
}
if (y && deltaY !== 0) {
top = $elem.scrollTop()
if ((top === 0 && deltaY < 0) ||
(top >= that.maxY && deltaY > 0)) {
return options.wheelPropagation
}
}
}
}
var events = {
scroll: function() {
// TODO: IE 滚动条抖动
$elem.on('scroll' + ns, function() {
that.update()
})
},
wheel: function() {
// bug: type=1 时悬停在滚动条上不能滚动
// https://developer.mozilla.org/en-US/docs/Web/Reference/Events/wheel
var wheel = ('onwheel' in document || document.documentMode > 8) ? 'wheel' :
'onmousewheel' in document ? 'mousewheel' : 'DOMMouseScroll'
$elem.on(wheel + ns, function(e) {
var left = $elem.scrollLeft()
var top = $elem.scrollTop()
var ev = e.originalEvent
var delta, deltaX, deltaY
// 只关注滚动方向
// TODO: 没有双向滚轴的鼠标,需要测试
switch(e.type) {
case 'wheel':
deltaX = ev.deltaX
deltaY = ev.deltaY
break
case 'mousewheel':
deltaX = ev.wheelDeltaX && -ev.wheelDeltaX
deltaY = -ev.wheelDelta
break
case 'DOMMouseScroll':
deltaY = ev.detail
break
}
deltaX = deltaX < 0 ? -1 : deltaX > 0 ? 1 : 0
deltaY = deltaY < 0 ? -1 : deltaY > 0 ? 1 : 0
// 两个滚轴各管各的滚动条
if (!options.wheelBoth) {
x && $elem.scrollLeft(left + deltaX * speed)
// y && $elem.scrollTop(top + deltaY * speed)
this.scrollTop += deltaY * speed
}
// 两个滚轴都支持水平滚动条,前提是只有水平滚动条
else if (x && !y) {
delta = deltaX || deltaY
$elem.scrollLeft(left + delta * speed)
} else if (y && !x) {
delta = deltaY || deltaX
$elem.scrollTop(top + delta * speed)
}
!isPropagated(deltaX, deltaY) && e.preventDefault()
})
},
click: function() {
// TODO: 滚动距离
var click = 'click' + ns
if (hasX) {
$barX.on(click, function(e) {
var delta = e.pageX - $thumbX.offset().left
var left = $elem.scrollLeft()
if (delta < 0) {
$elem.scrollLeft(left - speed)
} else if (delta > 0) {
$elem.scrollLeft(left + speed)
}
return false
})
$thumbX.on(click, function(e) {
e.stopPropagation()
})
}
if (hasY) {
$barY.on(click, function(e) {
var delta = e.pageY - $thumbY.offset().top
var top = $elem.scrollTop()
if (delta < 0) {
$elem.scrollTop(top - speed)
} else if (delta > 0) {
$elem.scrollTop(top + speed)
}
return false
})
$thumbY.on(click, function(e) {
e.stopPropagation()
})
}
},
drag: function() {
var md = 'mousedown' + ns
var mm = 'mousemove' + ns
var mu = 'mouseup' + ns
var start = {}
var current = {}
var klass = 'scrolling'
function moveX(deltaX) {
$elem.scrollLeft($elem.scrollLeft() + parseInt(deltaX / that.ratioX, 10))
}
function moveY(deltaY) {
$elem.scrollTop($elem.scrollTop() + parseInt(deltaY / that.ratioY, 10))
}
if (hasX) {
$barX.on(md, function (e) {
start.pageX = e.pageX
$barX.addClass(klass)
return false
})
$doc
.on(mm, function (e) {
if ($barX.hasClass(klass)) {
current.pageX = e.pageX
moveX(current.pageX - start.pageX)
start.pageX = current.pageX
return false
}
})
.on(mu, function (e) {
if ($barX.hasClass(klass)) {
$barX.removeClass(klass)
}
})
}
if (hasY) {
$barY.on(md, function (e) {
start.pageY = e.pageY
$barY.addClass(klass)
return false
})
$doc
.on(mm, function (e) {
if ($barY.hasClass(klass)) {
current.pageY = e.pageY
moveY(current.pageY - start.pageY)
start.pageY = current.pageY
return false
}
})
.on(mu, function (e) {
if ($barY.hasClass(klass)) {
$barY.removeClass(klass)
}
})
}
},
keyboard: function() {
// bug: 页面 iframe 有滚动条时,方向键控制的是它的滚动条
var me = 'mouseenter' + ns
var ml = 'mouseleave' + ns
var kd = 'keydown' + ns
var hovered = false;
$elem
.on(me, function() {
hovered = true;
})
.on(ml, function() {
hovered = false;
});
var shouldPrevent = false;
$doc.on(kd, function (e) {
if (!hovered) return
var deltaX = 0
var deltaY = 0
switch (e.which) {
case 37: // left
deltaX = -1;
break;
case 38: // up
deltaY = -1;
break;
case 39: // right
deltaX = 1;
break;
case 40: // down
deltaY = 1;
break;
default:
return;
}
x && $elem.scrollLeft($elem.scrollLeft() + deltaX * speed)
y && $elem.scrollTop($elem.scrollTop() + deltaY * speed)
!isPropagated(deltaX, deltaY) && e.preventDefault()
})
},
// 移动端浏览器不大支持
//
touch: function() {
// var ts = 'touchstart' + ns
// var tm = 'touchmove' + ns
// var te = 'touchend' + ns
// var start = {}
// var touchSpeed = {}
// var breakingProcess = null
// var inGlobalTouch = false
// var move = function (deltaX, deltaY) {
// // 触摸是反向移动
// $elem.scrollLeft($elem.scrollLeft() - deltaX)
// $elem.scrollTop($elem.scrollTop() - deltaY)
// }
// $(window)
// .on(ts, function (e) {
// inGlobalTouch = true
// })
// .on(te, function (e) {
// inGlobalTouch = false
// });
// $elem.
// on(ts, function (e) {
// var touch = e.originalEvent.targetTouches[0]
// start.pageX = touch.pageX
// start.pageY = touch.pageY
// start.time = $.now()
// if (breakingProcess !== null) {
// clearInterval(breakingProcess)
// }
// e.stopPropagation()
// })
// .on(tm, function (e) {
// if (!inGlobalTouch && e.originalEvent.targetTouches.length === 1) {
// var touch = e.originalEvent.targetTouches[0]
// var current = {}
// var deltax, deltaY
// current.pageX = touch.pageX
// current.pageY = touch.pageY
// deltaX = current.pageX - start.pageX
// deltaY = current.pageY - start.pageY
// move(deltaX, deltaY)
// current.time = $.now()
// touchSpeed.x = deltaX / (current.time - start.time)
// touchSpeed.y = deltaY / (current.time - start.time)
// start = current
// e.preventDefault()
// }
// })
// .on(te, function (e) {
// clearInterval(breakingProcess)
// breakingProcess = setInterval(function () {
// if (Math.abs(touchSpeed.x) < 0.01 && Math.abs(touchSpeed.y) < 0.01) {
// clearInterval(breakingProcess)
// return
// }
// move(touchSpeed.x * 30, touchSpeed.y * 30)
// touchSpeed.x *= 0.8
// touchSpeed.y *= 0.8
// }, 10);
// });
}
}
events.scroll()
events.wheel()
events.click()
events.drag()
options.keyboard && events.keyboard()
// if ('ontouchstart' in window) events.touch()
},
destroy: function() {
var ns = this.namespace
// TODO: 待完善
this.$elem.data('scrollbar', '').off(ns)
this.options.keyboard && $(document).off(ns)
}
})
$.fn.scrollbar = function(options) {
return this.each(function() {
var $this = $(this)
var data = $this.data('scrollbar')
var settings = $.extend({}, Scrollbar.defaults, typeof options === 'object' && options)
if (!data) $this.data('scrollbar', (data = new Scrollbar(this, settings)))
data._init()
})
}
$.fn.scrollbar.Constructor = Scrollbar
}))