-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge-scroll.js
171 lines (144 loc) · 4.36 KB
/
edge-scroll.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
(function () {
function EdgeScroll (radius, speed) {
this.radius = radius //distance from the edge to start scrolling
this.speed = speed // pixels per second
this.scrolling = false
this.proximity = null
this.clientX = null
this.clientY = null
this.scrollX = window.scrollX
this.scrollY = window.scrollY
this.innerWidth = window.innerWidth
this.innerHeight = window.innerHeight
this.handlers = []
this.deadzones = []
this.clampedVelocity = function () {
var N = 6
var p = this.proximity / this.radius
return Math.pow(p * -1/2 + 1, N)
}
this.scrollTick = (function (time) {
var elapsed = this.lastTime ? time - this.lastTime : 0
this.lastTime = time
if (this.scrolling === false) {
return this.lastTime = 0
}
var scrollByX = 0
var scrollByY = 0
var centerX = this.innerWidth / 2
var centerY = this.innerHeight / 2
var headingX = this.clientX - centerX
var headingY = this.clientY - centerY
var magnitude = Math.sqrt(Math.pow(headingX, 2) + Math.pow(headingY, 2))
var normalizedX = headingX / magnitude
var normalizedY = headingY / magnitude
var velocityX = normalizedX * this.speed * this.clampedVelocity()
var velocityY = normalizedY * this.speed * this.clampedVelocity()
if (this.clientX <= radius || (this.innerWidth - this.clientX) <= radius) {
scrollByX = Math.round(velocityX * elapsed / 1000)
}
if (this.clientY <= radius || (this.innerHeight - this.clientY) <= radius) {
scrollByY = Math.round(velocityY * elapsed / 1000)
}
window.scrollBy(scrollByX, scrollByY)
this.scrollX = window.scrollX
this.scrollY = window.scrollY
this.innerWidth = window.innerWidth
this.innerHeight = window.innerHeight
window.requestAnimationFrame(this.scrollTick)
}).bind(this)
this.scroll = function (x, y) {
window.scroll(x, y)
this.scrollX = window.scrollX
this.scrollY = window.scrollY
this.innerWidth = window.innerWidth
this.innerHeight = window.innerHeight
}
this.onScroll = function (handler) {
this.handlers.push(handler)
}
this.offScroll = function (handler) {
var i = this.handlers.indexOf(handler)
if (i > -1) {
this.handlers.splice(i, 1)
}
}
var scroll = this
document.addEventListener('mouseout', function (evt) {
if (evt.relatedTarget === null) {
scroll.scrolling = false
}
})
document.addEventListener('mousemove', function (evt) {
scroll.clientX = evt.clientX
scroll.clientY = evt.clientY
var px1 = evt.clientX - 0
var px2 = scroll.innerWidth - evt.clientX
var px3 = evt.clientY - 0
var px4 = scroll.innerHeight - evt.clientY
scroll.proximity = Math.min(px1, px2, px3, px4)
if (scroll.proximity < scroll.radius) {
if (scroll.dead) {
return
}
if (!scroll.scrolling) {
scroll.scrolling = true
window.requestAnimationFrame(scroll.scrollTick)
}
} else {
scroll.scrolling = false
}
})
document.addEventListener('scroll', function () {
var src
if (scroll.scrolling) {
src = scroll
} else {
src = window
}
var scrollX = src.scrollX
var scrollY = src.scrollY
var innerWidth = src.innerWidth
var innerHeight = src.innerHeight
scroll.handlers.forEach(function (handler) {
handler(scrollX, scrollY, innerWidth, innerHeight)
})
})
window.addEventListener('resize', function () {
// not working in Chrome
//scroll.innerWidth = window.innerWidth
//scroll.innerHeight = window.innerHeight
scroll.innerWidth = document.documentElement.clientWidth
scroll.innerHeight = document.documentElement.clientHeight
})
this.addDeadzone = function (elem) {
function enterDeadzone () {
scroll.dead = true
scroll.scrolling = false
}
function leaveDeadzone () {
scroll.dead = false
}
scroll.deadzones.push({
elem: elem,
enter: enterDeadzone,
leave: leaveDeadzone
})
elem.addEventListener('mouseenter', enterDeadzone)
elem.addEventListener('mouseleave', leaveDeadzone)
}
this.removeDeadzone = function (elem) {
this.deadzones.forEach(function (deadzone) {
if (deadzone.elem === elem) {
elem.removeEventListener('mouseenter', deadzone.enter)
elem.removeEventListener('mouseleave', deadzone.leave)
}
})
}
}
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = EdgeScroll
} else {
window.EdgeScroll = EdgeScroll
}
})()