forked from RubyLouvre/avalon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavalon.iscroll.js
1178 lines (991 loc) · 42.2 KB
/
avalon.iscroll.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
define(["avalon"], function() {
var rAF = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
var noScroll = avalon.oneObject("TEXTAREA,INPUT,SELECT")
var utils = {
ease: {
quadratic: {
style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
fn: function(k) {
return k * (2 - k);
}
},
circular: {
style: 'cubic-bezier(0.1, 0.57, 0.1, 1)', // Not properly "circular" but this looks better, it should be (0.075, 0.82, 0.165, 1)
fn: function(k) {
return Math.sqrt(1 - (--k * k));
}
},
back: {
style: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)',
fn: function(k) {
var b = 4;
return (k = k - 1) * k * ((b + 1) * k + b) + 1;
}
},
bounce: {
style: '',
fn: function(k) {
if ((k /= 1) < (1 / 2.75)) {
return 7.5625 * k * k;
} else if (k < (2 / 2.75)) {
return 7.5625 * (k -= (1.5 / 2.75)) * k + 0.75;
} else if (k < (2.5 / 2.75)) {
return 7.5625 * (k -= (2.25 / 2.75)) * k + 0.9375;
} else {
return 7.5625 * (k -= (2.625 / 2.75)) * k + 0.984375;
}
}
},
elastic: {
style: '',
fn: function(k) {
var f = 0.22,
e = 0.4;
if (k === 0) {
return 0;
}
if (k === 1) {
return 1;
}
return (e * Math.pow(2, -10 * k) * Math.sin((k - f / 4) * (2 * Math.PI) / f) + 1);
}
}
},
offset: function(el) {
var left = -el.offsetLeft,
top = -el.offsetTop;
// jshint -W084
while (el = el.offsetParent) {
left -= el.offsetLeft;
top -= el.offsetTop;
}
// jshint +W084
return {
left: left,
top: top
};
},
momentum: function(current, start, time, lowerMargin, wrapperSize, deceleration) {
var distance = current - start,
speed = Math.abs(distance) / time,
destination,
duration;
deceleration = deceleration === void 0 ? 0.0006 : deceleration;
destination = current + (speed * speed) / (2 * deceleration) * (distance < 0 ? -1 : 1)
duration = speed / deceleration
if (destination < lowerMargin) {
destination = wrapperSize ? lowerMargin - (wrapperSize / 2.5 * (speed / 8)) : lowerMargin
distance = Math.abs(destination - current)
duration = distance / speed
} else if (destination > 0) {
destination = wrapperSize ? wrapperSize / 2.5 * (speed / 8) : 0
distance = Math.abs(current) + destination
duration = distance / speed
}
return {
destination: Math.round(destination),
duration: duration
};
},
isBadAndroid: /Android /.test(window.navigator.appVersion) && !(/Chrome\/\d/.test(window.navigator.appVersion)),
style: {
transform: avalon.cssName("transform"),
transitionTimingFunction: avalon.cssName("transitionTimingFunction"),
transitionDuration: avalon.cssName("transitionDuration"),
transitionDelay: avalon.cssName("transitionDelay"),
transformOrigin: avalon.cssName("transformOrigin")
},
addEvent: function(el, type, fn, capture) {
el.addEventListener(type, fn, !!capture);
},
removeEvent: function(el, type, fn, capture) {
el.removeEventListener(type, fn, !!capture);
}
}
if (!window.addEventListener) {
utils.addEvent = function(el, type, fn, capture) {
el.attachEvent("on" + type, fn)
}
utils.removeEvent = function(el, type, fn, capture) {
el.detachEvent("on" + type, fn)
}
}
avalon.mix(utils, {
hasTransform: !!avalon.cssName("transform"),
hasPerspective: !!avalon.cssName("perspective"),
hasTransition: !!avalon.cssName("transition")
})
var touchNames = ["mousedown", "mousemove", "mouseup", ""]
var IE11touch = navigator.pointerEnabled
var IE9_10touch = navigator.msPointerEnabled
if (IE11touch) { //IE11 与 W3C
touchNames = ["pointerdown", "pointermove", "pointerup", "pointercancel"]
} else if (IE9_10touch) { //IE9-10
touchNames = ["MSPointerDown", "MSPointerMove", "MSPointerUp", "MSPointerCancel"]
} else if ("ontouchstart" in window) {
touchNames = ["touchstart", "touchmove", "touchend", "touchcancel"]
}
var Passthrough = {
vertical: "vertical",
horizontal: "horizontal",
true: "vertical"
}
//https://www.gitbook.io/book/iiunknown/iscroll-5-api-cn/reviews
function IScroll(el, options) {
this.wrapper = typeof el === 'string' ? document.querySelector(el) : el;
this.scroller = this.wrapper.children[0];
this.scrollerStyle = this.scroller.style;
//默认参数
this.options = {
mouseWheelSpeed: 20,
snapThreshold: 0.334,
startX: 0,
startY: 0,
scrollY: true,
directionLockThreshold: 5,
momentum: true, //动量效果,拖动惯性;关闭此功能将大幅度提升性能。
bounce: true, //当滚动器到达容器边界时他将执行一个小反弹动画。在老的或者性能低的设备上禁用反弹对实现平滑的滚动有帮助。
bounceTime: 600,
bounceEasing: '',
preventDefault: true,
HWCompositing: true, //开启CSS3硬件加速(通过translateZ(0)实现)
useTransition: true,
useTransform: true,
scrollbars: false, //是否出现滚动条
fadeScrollbars: false, //不想使用滚动条淡入淡出方式时,需要设置此属性为false以便节省资源。
interactiveScrollbars: false, //此属性可以让滚动条能拖动,用户可以与之交互。
//滚动条尺寸改变基于容器和滚动区域的宽/高之间的比例。此属性设置为false让滚动条固定大小。
//这可能有助于自定义滚动条样式(参考下面)。
resizeScrollbars: true
};
avalon.mix(this.options, options)
// 调整参数
this.translateZ = this.options.HWCompositing && utils.hasPerspective ? ' translateZ(0)' : '';
//使用CSS transition来实现动画效果(动量和弹力)。如果设置为false,那么将使用requestAnimationFrame代替。
//在现在浏览器中这两者之间的差异并不明显。在老的设备上transitions执行得更好。
this.options.useTransition = utils.hasTransition && this.options.useTransition
//默认情况下引擎会使用CSStransform属性。如果现在还是2007年,那么可以设置这个属性为false,这就是说:引擎将使
//用top/left属性来进行滚动。
//这个属性在滚动器感知到Flash,iframe或者视频插件内容时会有用,但是需要注意:性能会有极大的损耗。
this.options.useTransform = utils.hasTransform && this.options.useTransform;
// 决定哪一个滚动条使用原生滚动条
// 当其值为horizontal时,横向为人工的,纵向为原生的;
// 当其值为vertical时,横向为原生的,纵向为人工的;
// 它只有horizontal,vertical,undefined三种值
var ep = this.options.eventPassthrough
ep = this.options.eventPassthrough = Passthrough[ep]
// 默认情况下scrollY为true, scrollX为false,因此只出现纵向滚动条,想出现横向滚动条,需要设置scrollX = true
this.options.scrollY = ep === "vertical" ? false : !!this.options.scrollY
this.options.scrollX = ep === "horizontal" ? false : !!this.options.scrollX
this.options.preventDefault = !this.options.eventPassthrough && !!this.options.preventDefault;
// 此属性针对于两个两个纬度的滚动条(当你需要横向和纵向滚动条)。通常情况下你开始滚动一个方向上的滚动条,另外一
// 个方向上会被锁定不动。有些时候,你需要无约束的移动(横向和纵向可以同时响应),在这样的情况下此属性需要设置
// 为true。默认值:false
this.options.freeScroll = !!(this.options.freeScroll && !this.options.eventPassthrough);
this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
this.options.bounceEasing = typeof this.options.bounceEasing === 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
this.options.resizePolling = this.options.resizePolling === void 0 ? 60 : this.options.resizePolling;
if (this.options.tap === true) {
this.options.tap = 'tap';
}
if (this.options.shrinkScrollbars == 'scale') {
this.options.useTransition = false;
}
this.options.invertWheelDirection = this.options.invertWheelDirection ? -1 : 1;
// INSERT POINT: NORMALIZATION
// Some defaults
this.x = 0;
this.y = 0;
this.directionX = 0;
this.directionY = 0;
this._events = {};
// INSERT POINT: DEFAULTS
this._init();
this.refresh();
this.scrollTo(this.options.startX, this.options.startY);
this.enable();
}
avalon.IScroll = IScroll
function getTransitionEndEventName() {
var obj = {
TransitionEvent: "transitionend",
WebKitTransitionEvent: "webkittransitionEnd",
OTransitionEvent: "OTransitionEnd",
otransitionEvent: "otransitionEnd",
MSTransitionEvent: "MSTransitionEnd"
}
// var ev = document.createEvent("TransitionEvent"); // FIXME: un-specified
// ev.initTransitionEvent("transitionend", true, true, "some-unknown-prop", -4.75);
// document.body.dispatchEvent(ev);
var ret = false, ev
for (var name in obj) {
try {
ev = document.createEvent(name)//只有firefox不支持
ret = obj[name]
break
} catch (e) {
}
}
if (ret === false) {
//https://bugzilla.mozilla.org/show_bug.cgi?id=868751
//https://hg.mozilla.org/integration/mozilla-inbound/rev/a20ea0d494a0
try {
ev = new TransitionEvent("transitionend",
{
bubbles: true,
cancelable: true,
propertyName: "some-unknown-prop",
elapsedTime: 0.5,
pseudoElement: "pseudo"
});
ret = "transitionend"
} catch (e) {
}
}
getTransitionEndEventName = function() {
return ret
}
return ret
}
function fixEvent(event) {
var ret = {}
for (var i in event) {
ret[i] = event[i]
}
var target = ret.target = event.srcElement
if (event.type.indexOf("key") === 0) {
ret.which = event.charCode != null ? event.charCode : event.keyCode
} else if (/mouse|click/.test(event.type)) {
var doc = target.ownerDocument || DOC
var box = doc.compatMode === "BackCompat" ? doc.body : doc.documentElement
ret.pageX = event.clientX + (box.scrollLeft >> 0) - (box.clientLeft >> 0)
ret.pageY = event.clientY + (box.scrollTop >> 0) - (box.clientTop >> 0)
}
ret.timeStamp = new Date - 0
ret.originalEvent = event
ret.preventDefault = function() { //阻止默认行为
event.returnValue = false
}
ret.stopPropagation = function() { //阻止事件在DOM树中的传播
event.cancelBubble = true
}
return ret
}
IScroll.prototype = {
version: '5.1.2',
_init: function() {
this._initEvents()
console.log(this.options.scrollbars || this.options.indicators)
if (this.options.scrollbars || this.options.indicators) {
// this._initIndicators();
}
if (this.options.mouseWheel) {
this._initWheel();
}
if (this.options.snap) {
// this._initSnap();
}
if (this.options.keyBindings) {
// this._initKeys();
}
},
destroy: function() {
this._initEvents(true);
this._execEvent('destroy');
},
_transitionEnd: function(e) {
if (e.target !== this.scroller || !this.isInTransition) {
return;
}
this._transitionTime();
if (!this.resetPosition(this.options.bounceTime)) {
this.isInTransition = false;
this._execEvent('scrollEnd');
}
},
refresh: function() {
var rf = this.wrapper.offsetHeight; // Force reflow
this.wrapperWidth = this.wrapper.clientWidth;
this.wrapperHeight = this.wrapper.clientHeight;
/* REPLACE START: refresh */
this.scrollerWidth = this.scroller.offsetWidth;
this.scrollerHeight = this.scroller.offsetHeight;
this.maxScrollX = this.wrapperWidth - this.scrollerWidth;
this.maxScrollY = this.wrapperHeight - this.scrollerHeight;
/* REPLACE END: refresh */
this.hasHorizontalScroll = this.options.scrollX && this.maxScrollX < 0;
this.hasVerticalScroll = this.options.scrollY && this.maxScrollY < 0;
if (!this.hasHorizontalScroll) {
this.maxScrollX = 0;
this.scrollerWidth = this.wrapperWidth;
}
if (!this.hasVerticalScroll) {
this.maxScrollY = 0;
this.scrollerHeight = this.wrapperHeight;
}
this.endTime = 0;
this.directionX = 0;
this.directionY = 0;
this.wrapperOffset = utils.offset(this.wrapper);
this._execEvent('refresh');
this.resetPosition();
// INSERT POINT: _refresh
},
_initEvents: function(remove) {
//绑定或卸载事件
var eventType = remove ? utils.removeEvent : utils.addEvent
var target = this.options.bindToWrapper ? this.wrapper : window
var that = this
function hander(e) {
if (!("target" in e)) {
e = fixEvent(e)
}
that.handleEvent(e)
}
eventType(window, 'onorientationchange' in window ? "orientationchange" : "resize", hander)
if (this.options.click) {
eventType(this.wrapper, "click", hander, true)
}
for (var i = 0, type; type = touchNames[i]; i++) {
var el = i === 0 ? this.wrapper : target
eventType(el, type, hander)
}
eventType(this.scroller, getTransitionEndEventName(), hander)
},
handleEvent: function(e) {
switch (e.type) {
case touchNames[0]:
this._start(e)
break
case touchNames[1]:
this._move(e)
break
case touchNames[2]:
case touchNames[3]:
this._end(e)
break
case "orientationchange":
case "resize":
this._resize()
break
case getTransitionEndEventName():
this._transitionEnd(e)
break
case "keydown":
this._key(e)
break
case "click":
if (!e._constructed) {
e.preventDefault()
e.stopPropagation()
}
break
}
},
on: function(type, fn) {//添加用户回调
if (!this._events[type]) {
this._events[type] = []
}
this._events[type].push(fn)
},
off: function(type, fn) {//卸载用户回调
var fns = this._events[type] || []
var index = fns.indexOf(fn)
if (index > -1) {
this._events[type].splice(index, 1)
}
},
_execEvent: function(type) {//触发用户回调
var fns = this._events[type] || []
var args = [].slice.call(arguments, 1)
for (var i = 0, fn; fn = fns[i++]; ) {
fn.apply(this, args)
}
},
_start: function(e) {
if (!this.enabled || this.initiated || e.button !== 0) {
return//只处理左键
}
if (this.options.preventDefault && !utils.isBadAndroid && !noScroll[e.target.tagName]) {
if (!this.options.mouseWheel) {
e.preventDefault();
}
}
var point = e.touches ? e.touches[0] : e
this.initiated = true
this.moved = false;
this.distX = 0;
this.distY = 0;
this.directionX = 0;
this.directionY = 0;
this.directionLocked = 0;
this._transitionTime()
this.startTime = new Date - 0
if (this.options.useTransition && this.isInTransition) {
this.isInTransition = false;
var pos = this.getComputedPosition();
this._translate(Math.round(pos.x), Math.round(pos.y));
this._execEvent('scrollEnd');
} else if (!this.options.useTransition && this.isAnimating) {
this.isAnimating = false;
this._execEvent('scrollEnd');
}
this.startX = this.x;
this.startY = this.y;
this.absStartX = this.x;
this.absStartY = this.y;
this.pointStartX = point.pageX;
this.pointStartY = point.pageY;
this.pointX = point.pageX;
this.pointY = point.pageY;
this._execEvent('beforeScrollStart') //
},
_move: function(e) {
if (!this.initiated) {
return;
}
if (this.options.preventDefault) { // increases performance on Android? TODO: check!
e.preventDefault();
}
var point = e.touches ? e.touches[0] : e,
deltaX = point.pageX - this.pointStartX,
deltaY = point.pageY - this.pointStartY,
timestamp = new Date - 0,
newX, newY,
absDistX, absDistY;
this.pointX = point.pageX;//当前相对于页面的坐标
this.pointY = point.pageY;
this.distX = deltaX;
this.distY = deltaY;
absDistX = Math.abs(this.distX);
absDistY = Math.abs(this.distY);
// We need to move at least 10 pixels for the scrolling to initiate
if (timestamp - this.endTime > 300 && (absDistX < 10 && absDistY < 10)) {
return;
}
// If you are scrolling in one direction lock the other
if (!this.directionLocked && !this.options.freeScroll) {
if (absDistX > absDistY + this.options.directionLockThreshold) {
this.directionLocked = 'h'; // lock horizontally
} else if (absDistY >= absDistX + this.options.directionLockThreshold) {
this.directionLocked = 'v'; // lock vertically
} else {
this.directionLocked = 'n'; // no lock
}
}
if (this.directionLocked === 'h') {
if (this.options.eventPassthrough === 'vertical') {
e.preventDefault();
} else if (this.options.eventPassthrough === 'horizontal') {
this.initiated = false;
return;
}
deltaY = 0;
} else if (this.directionLocked === 'v') {
if (this.options.eventPassthrough === 'horizontal') {
e.preventDefault();
} else if (this.options.eventPassthrough === 'vertical') {
this.initiated = false;
return;
}
deltaX = 0;
}
deltaX = this.hasHorizontalScroll ? deltaX : 0;
deltaY = this.hasVerticalScroll ? deltaY : 0;
newX = this.x + deltaX;
newY = this.y + deltaY;
// Slow down if outside of the boundaries
if (newX > 0 || newX < this.maxScrollX) {
newX = this.options.bounce ? this.x + deltaX / 3 : newX > 0 ? 0 : this.maxScrollX;
}
if (newY > 0 || newY < this.maxScrollY) {
newY = this.options.bounce ? this.y + deltaY / 3 : newY > 0 ? 0 : this.maxScrollY;
}
this.directionX = deltaX > 0 ? -1 : deltaX < 0 ? 1 : 0;
this.directionY = deltaY > 0 ? -1 : deltaY < 0 ? 1 : 0;
if (!this.moved) {
this._execEvent('scrollStart');
}
this.moved = true;
this._translate(newX, newY);
/* REPLACE START: _move */
if (timestamp - this.startTime > 300) {
this.startTime = timestamp;
this.startX = this.x;
this.startY = this.y;
}
/* REPLACE END: _move */
},
_end: function(e) {
if (!this.enabled) {
return;
}
if (this.options.preventDefault && !noScroll[e.target.tagName]) {
e.preventDefault();
}
var point = e.changedTouches ? e.changedTouches[0] : e,
momentumX,
momentumY,
duration = new Date - this.startTime,
newX = Math.round(this.x),
newY = Math.round(this.y),
distanceX = Math.abs(newX - this.startX),
distanceY = Math.abs(newY - this.startY),
time = 0,
easing = '';
this.isInTransition = 0;
this.initiated = false;
this.endTime = new Date - 0;
// reset if we are outside of the boundaries
if (this.resetPosition(this.options.bounceTime)) {
return;
}
this.scrollTo(newX, newY); // ensures that the last position is rounded
// we scrolled less than 10 pixels
if (!this.moved) {
this._execEvent('scrollCancel');
return;
}
// start momentum animation if needed
if (this.options.momentum && duration < 300) {
momentumX = this.hasHorizontalScroll ? utils.momentum(this.x, this.startX, duration, this.maxScrollX, this.options.bounce ? this.wrapperWidth : 0, this.options.deceleration) : {destination: newX, duration: 0};
momentumY = this.hasVerticalScroll ? utils.momentum(this.y, this.startY, duration, this.maxScrollY, this.options.bounce ? this.wrapperHeight : 0, this.options.deceleration) : {destination: newY, duration: 0};
newX = momentumX.destination;
newY = momentumY.destination;
time = Math.max(momentumX.duration, momentumY.duration);
this.isInTransition = 1;
}
// INSERT POINT: _end
if (newX !== this.x || newY !== this.y) {
// change easing function when scroller goes out of the boundaries
if (newX > 0 || newX < this.maxScrollX || newY > 0 || newY < this.maxScrollY) {
easing = utils.ease.quadratic;
}
this.scrollTo(newX, newY, time, easing);
return;
}
this._execEvent('scrollEnd');
},
_translate: function(x, y) {
if (this.options.useTransform) {//使用transform
this.scrollerStyle[utils.style.transform] = 'translate(' + x + 'px,' + y + 'px)' + this.translateZ;
} else {
x = Math.round(x);//使用translate
y = Math.round(y);
this.scrollerStyle.left = x + 'px';
this.scrollerStyle.top = y + 'px';
}
this.x = x;
this.y = y;
},
_animate: function(destX, destY, duration, easingFn) {
var that = this,
startX = this.x,
startY = this.y,
startTime = utils.getTime(),
destTime = startTime + duration;
function step() {
var now = utils.getTime(),
newX, newY,
easing;
if (now >= destTime) {
that.isAnimating = false;
that._translate(destX, destY);
if (!that.resetPosition(that.options.bounceTime)) {
that._execEvent('scrollEnd');
}
return;
}
now = (now - startTime) / duration;
easing = easingFn(now);
newX = (destX - startX) * easing + startX;
newY = (destY - startY) * easing + startY;
that._translate(newX, newY);
if (that.isAnimating) {
rAF(step);
}
}
this.isAnimating = true;
step();
},
_transitionTime: function(time) {
//调整时长
time = time || 0
this.scrollerStyle[utils.style.transitionDuration] = time + 'ms';
if (!time && utils.isBadAndroid) {
this.scrollerStyle[utils.style.transitionDuration] = '0.001s';
}
},
_transitionTimingFunction: function(easing) {
//设置缓动公式
this.scrollerStyle[utils.style.transitionTimingFunction] = easing;
},
getComputedPosition: function() {
var matrix = window.getComputedStyle(this.scroller, null),
x, y;
if (this.options.useTransform) {
matrix = matrix[utils.style.transform].split(')')[0].split(', ');
x = +(matrix[12] || matrix[4]);
y = +(matrix[13] || matrix[5]);
} else {
x = +matrix.left.replace(/[^-\d.]/g, '');
y = +matrix.top.replace(/[^-\d.]/g, '');
}
return {x: x, y: y};
},
_resize: function() {
var that = this;
clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(function() {
that.refresh();
}, this.options.resizePolling);
},
resetPosition: function(time) {
var x = this.x,
y = this.y;
time = time || 0;
if (!this.hasHorizontalScroll || this.x > 0) {
x = 0;
} else if (this.x < this.maxScrollX) {
x = this.maxScrollX;
}
if (!this.hasVerticalScroll || this.y > 0) {
y = 0;
} else if (this.y < this.maxScrollY) {
y = this.maxScrollY;
}
if (x === this.x && y === this.y) {
return false;
}
this.scrollTo(x, y, time, this.options.bounceEasing);
return true;
},
//调用这个方法会立即停止动画滚动,并且把滚动位置还原成0,取消绑定touchmove, touchend、touchcancel事件。
disable: function() {
this.enabled = false;
},
//调用这个方法,使得iscroll恢复默认正常状态
enable: function() {
this.enabled = true;
},
/*********************************************************************
* 滚动到目标位置 *
**********************************************************************/
scrollBy: function(x, y, time, easing) {
x = this.x + x;
y = this.y + y;
time = time || 0;
this.scrollTo(x, y, time, easing);
},
//这个方法接受4个参数 x, y, time, easing x 为移动的x轴坐标,y为移动的y轴坐标, time为移动时间,easing表示使用何种缓动公式。
scrollTo: function(x, y, time, easing) {
easing = easing || utils.ease.circular;
this.isInTransition = this.options.useTransition && time > 0;// 正在使用CSS3transition
if (!time || (this.options.useTransition && easing.style)) {
this._transitionTimingFunction(easing.style);
this._transitionTime(time);
this._translate(x, y);
} else {//如果不支持,使用JS动画
this._animate(x, y, time, easing.fn);
}
},
// scrollToElement --> scrollTo --> _translate or _animate
// scrollBy --> scrollTo --> _translate or _animate
scrollToElement: function(el, time, offsetX, offsetY, easing) {
el = el.nodeType ? el : this.scroller.querySelector(el);
if (!el) {
return;
}
var pos = utils.offset(el);
pos.left -= this.wrapperOffset.left;
pos.top -= this.wrapperOffset.top;
// if offsetX/Y are true we center the element to the screen
if (offsetX === true) {
offsetX = Math.round(el.offsetWidth / 2 - this.wrapper.offsetWidth / 2);
}
if (offsetY === true) {
offsetY = Math.round(el.offsetHeight / 2 - this.wrapper.offsetHeight / 2);
}
pos.left -= offsetX || 0;
pos.top -= offsetY || 0;
pos.left = pos.left > 0 ? 0 : pos.left < this.maxScrollX ? this.maxScrollX : pos.left;
pos.top = pos.top > 0 ? 0 : pos.top < this.maxScrollY ? this.maxScrollY : pos.top;
time = time == null || time === 'auto' ? Math.max(Math.abs(this.x - pos.left), Math.abs(this.y - pos.top)) : time;
this.scrollTo(pos.left, pos.top, time, easing);
},
/*********************************************************************
* 对齐处理 *
**********************************************************************/
_initWheel: function() {
var that = this
var removeFn = avalon.bind(this.wrapper, "mousewheel", function(e) {
if (!that.enabled) {
return;
}
e.preventDefault();
e.stopPropagation()
if (that.wheelTimeout === undefined) {
that._execEvent('scrollStart');
}
// Execute the scrollEnd event after 400ms the wheel stopped scrolling
clearTimeout(that.wheelTimeout);
that.wheelTimeout = setTimeout(function() {
that._execEvent('scrollEnd');
that.wheelTimeout = undefined;
}, 400);
if (e.wheelDeltaX === void 0) {
e.wheelDeltaY = e.wheelDelta
e.wheelDeltaX = 0
}
var wheelDeltaX = e.wheelDeltaX / 120 * that.options.mouseWheelSpeed
var wheelDeltaY = e.wheelDeltaY / 120 * that.options.mouseWheelSpeed
wheelDeltaX *= that.options.invertWheelDirection
wheelDeltaY *= that.options.invertWheelDirection
if (!that.hasVerticalScroll) {
wheelDeltaX = wheelDeltaY;
wheelDeltaY = 0;
}
if (that.options.snap) {
var newX = that.currentPage.pageX;
var newY = that.currentPage.pageY;
if (wheelDeltaX > 0) {
newX--;
} else if (wheelDeltaX < 0) {
newX++;
}
if (wheelDeltaY > 0) {
newY--;
} else if (wheelDeltaY < 0) {
newY++;
}
that.goToPage(newX, newY);
return;
}
newX = that.x + Math.round(that.hasHorizontalScroll ? wheelDeltaX : 0);
newY = that.y + Math.round(that.hasVerticalScroll ? wheelDeltaY : 0);
if (newX > 0) {
newX = 0;
} else if (newX < that.maxScrollX) {
newX = that.maxScrollX;
}
if (newY > 0) {
newY = 0;
} else if (newY < that.maxScrollY) {
newY = that.maxScrollY;
}
that.scrollTo(newX, newY, 0);
})
this.on('destroy', function() {
avalon.bind(this.wrapper, "mousewheel", removeFn)
});
},
/*********************************************************************
* Snap *
**********************************************************************/
_initSnap: function() {
this.currentPage = {};
if (typeof this.options.snap === 'string') {
this.options.snap = this.scroller.querySelectorAll(this.options.snap);
}
this.on('refresh', function() {
var i = 0, l,
m = 0, n,
cx, cy,
x = 0, y,
stepX = this.options.snapStepX || this.wrapperWidth,
stepY = this.options.snapStepY || this.wrapperHeight,
el;
this.pages = [];
if (!this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight) {
return;
}
if (this.options.snap === true) {
cx = Math.round(stepX / 2);
cy = Math.round(stepY / 2);
while (x > -this.scrollerWidth) {
this.pages[i] = [];
l = 0;
y = 0;
while (y > -this.scrollerHeight) {
this.pages[i][l] = {
x: Math.max(x, this.maxScrollX),
y: Math.max(y, this.maxScrollY),
width: stepX,
height: stepY,
cx: x - cx,
cy: y - cy
};
y -= stepY;
l++;
}
x -= stepX;
i++;
}
} else {
el = this.options.snap;
l = el.length;
n = -1;
for (; i < l; i++) {
if (i === 0 || el[i].offsetLeft <= el[i - 1].offsetLeft) {
m = 0;
n++;
}
if (!this.pages[m]) {
this.pages[m] = [];
}
x = Math.max(-el[i].offsetLeft, this.maxScrollX);
y = Math.max(-el[i].offsetTop, this.maxScrollY);
cx = x - Math.round(el[i].offsetWidth / 2);