@@ -25,21 +25,21 @@ const jumper = () => {
25
25
26
26
// scroll position helper
27
27
28
- function location ( ) {
28
+ function location ( ) {
29
29
return window . scrollY || window . pageYOffset
30
30
}
31
31
32
32
// element offset helper
33
33
34
- function top ( element ) {
34
+ function top ( element ) {
35
35
return element . getBoundingClientRect ( ) . top + start
36
36
}
37
37
38
38
// rAF loop helper
39
39
40
- function loop ( timeCurrent ) {
40
+ function loop ( timeCurrent ) {
41
41
// store time scroll started, if not started already
42
- if ( ! timeStart ) {
42
+ if ( ! timeStart ) {
43
43
timeStart = timeCurrent
44
44
}
45
45
@@ -54,18 +54,18 @@ const jumper = () => {
54
54
55
55
// check progress
56
56
timeElapsed < duration
57
- ? requestAnimationFrame ( loop ) // continue scroll loop
58
- : done ( ) // scrolling is done
57
+ ? window . requestAnimationFrame ( loop ) // continue scroll loop
58
+ : done ( ) // scrolling is done
59
59
}
60
60
61
61
// scroll finished helper
62
62
63
- function done ( ) {
63
+ function done ( ) {
64
64
// account for rAF time rounding inaccuracies
65
65
window . scrollTo ( 0 , start + distance )
66
66
67
67
// if scrolling to an element, and accessibility is enabled
68
- if ( element && a11y ) {
68
+ if ( element && a11y ) {
69
69
// add tabindex indicating programmatic focus
70
70
element . setAttribute ( 'tabindex' , '-1' )
71
71
@@ -74,7 +74,7 @@ const jumper = () => {
74
74
}
75
75
76
76
// if it exists, fire the callback
77
- if ( typeof callback === 'function' ) {
77
+ if ( typeof callback === 'function' ) {
78
78
callback ( )
79
79
}
80
80
@@ -84,59 +84,59 @@ const jumper = () => {
84
84
85
85
// API
86
86
87
- function jump ( target , options = { } ) {
87
+ function jump ( target , options = { } ) {
88
88
// resolve options, or use defaults
89
89
duration = options . duration || 1000
90
- offset = options . offset || 0
90
+ offset = options . offset || 0
91
91
callback = options . callback // "undefined" is a suitable default, and won't be called
92
- easing = options . easing || easeInOutQuad
93
- a11y = options . a11y || false
92
+ easing = options . easing || easeInOutQuad
93
+ a11y = options . a11y || false
94
94
95
95
// cache starting position
96
96
start = location ( )
97
97
98
98
// resolve target
99
- switch ( typeof target ) {
99
+ switch ( typeof target ) {
100
100
// scroll from current position
101
101
case 'number' :
102
102
element = undefined // no element to scroll to
103
- a11y = false // make sure accessibility is off
104
- stop = start + target
105
- break
103
+ a11y = false // make sure accessibility is off
104
+ stop = start + target
105
+ break
106
106
107
107
// scroll to element (node)
108
108
// bounding rect is relative to the viewport
109
109
case 'object' :
110
110
element = target
111
- stop = top ( element )
112
- break
111
+ stop = top ( element )
112
+ break
113
113
114
114
// scroll to element (selector)
115
115
// bounding rect is relative to the viewport
116
116
case 'string' :
117
117
element = document . querySelector ( target )
118
- stop = top ( element )
119
- break
118
+ stop = top ( element )
119
+ break
120
120
}
121
121
122
122
// resolve scroll distance, accounting for offset
123
123
distance = stop - start + offset
124
124
125
125
// resolve duration
126
- switch ( typeof options . duration ) {
126
+ switch ( typeof options . duration ) {
127
127
// number in ms
128
128
case 'number' :
129
129
duration = options . duration
130
- break
130
+ break
131
131
132
132
// function passed the distance of the scroll
133
133
case 'function' :
134
134
duration = options . duration ( distance )
135
- break
135
+ break
136
136
}
137
137
138
138
// start the loop
139
- requestAnimationFrame ( loop )
139
+ window . requestAnimationFrame ( loop )
140
140
}
141
141
142
142
// expose only the jump method
0 commit comments