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
|
var Scroller = (function(){
var Scroller = {}
var prev, next, burger
var webkitRatio = 1
Scroller.init = function(opt){
previous = opt.previous
next = opt.next
burger = opt.burger
if (is_mobile) {
// document.body.addEventListener("touchstart", touchstart)
// document.body.addEventListener("touchmove", touchmove)
// document.body.addEventListener("touchend", touchend)
}
else {
document.body.addEventListener("wheel", wheelDelta)
document.body.addEventListener("keydown", keydown)
}
}
var touching = false, touchStartTime, touchStartY, touchEndY, touchDist, touchScrollTop
function touchstart (e) {
touching = true
touchStartTime = +(new Date)
touchStartY = touchEndY = e.touches ? e.touches[0].pageY : e.pageY
}
function touchmove (e) {
if (! touching) return
e.preventDefault()
var timestamp = +(new Date)
var duration = timestamp - touchStartTime
touchEndY = e.touches ? e.touches[0].pageY : e.pageY
touchDist = touchStartY - touchEndY
}
function touchend (e) {
if (! touching) return
e.preventDefault()
touching = false
var timestamp = +(new Date)
var duration = timestamp - touchStartTime
touchDist = touchStartY - touchEndY
if (Math.abs(touchDist) < 10 || duration < 100) {
step(1)
}
else {
touchDist > 0 ? step(1) : step(-1)
}
}
function wheelDelta (e){
e.preventDefault()
var deltaY = 0
// WebKit
if ( e.deltaY ) {
deltaY += e.deltaY * webkitRatio
}
else if ( e.wheelDeltaY ) {
deltaY += e.wheelDeltaY * webkitRatio
}
// Opera / Explorer 9
else if ( e.wheelDelta ) {
deltaY += e.wheelDelta * webkitRatio
}
// Firefox
else if ( e.detail ) {
deltaY -= e.detail * 2
}
wheel(deltaY)
}
var lastScrollEvent = +new Date(), lastDY = 0, lastSign = 0
function wheel (dy) {
if (isNaN(dy)) return
var now = +new Date(), newSign = sign(dy)
if ((now > lastScrollEvent + 300) ||
(lastSign !== newSign && Math.abs(dy) > Math.abs(lastDY*2)) ||
(lastSign === newSign && Math.log(Math.abs(dy)) > 1+Math.log(Math.abs(lastDY)))) {
step(sign(dy))
}
lastDY = dy
lastSign = newSign
lastScrollEvent = now
}
function step (n) {
var now = +new Date()
if (now < lastScrollEvent + 300) {
return
}
lastScrollEvent = now
if (n < 0) {
previous()
}
else {
next()
}
}
function keydown (e) {
// console.log(e.keyCode)
switch (e.keyCode) {
case 27: // esc
e.preventDefault()
burger()
break
case 32: // space
e.preventDefault()
step(1)
break
case 36: // home
e.preventDefault()
break
case 35: // end
e.preventDefault()
break
case 33: // page up
case 38: // up
e.preventDefault()
step(-1)
break
case 34: // page down
case 40: // down
e.preventDefault()
step(1)
break
}
}
return Scroller
})()
|