Skip to content

Commit 9225c07

Browse files
committed
Decouple the scroll event from _scrollPage function using requestAnimationFrame.
1 parent df0703c commit 9225c07

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

dist/scrollReveal.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ window.scrollReveal = (function (window) {
3939

4040
'use strict';
4141

42+
/**
43+
* RequestAnimationFrame polyfill
44+
* @function
45+
* @private
46+
*/
47+
var requestAnimFrame = (function () {
48+
return window.requestAnimationFrame ||
49+
window.webkitRequestAnimationFrame ||
50+
window.mozRequestAnimationFrame ||
51+
function (callback) {
52+
window.setTimeout(callback, 1000 / 60);
53+
};
54+
}());
55+
4256
function scrollReveal(options) {
4357

4458
this.docElem = window.document.documentElement;
@@ -90,12 +104,13 @@ window.scrollReveal = (function (window) {
90104
self.update(el);
91105
});
92106

93-
var scrollHandler = function () {
107+
var scrollHandler = function (e) {
108+
// No changing, exit
94109
if (!self.scrolled) {
95110
self.scrolled = true;
96-
setTimeout(function () {
97-
self._scrollPage();
98-
}, 60);
111+
requestAnimFrame(function () {
112+
self._scrollPage
113+
});
99114
}
100115
};
101116

@@ -112,6 +127,7 @@ window.scrollReveal = (function (window) {
112127
self.resizeTimeout = setTimeout(delayed, 200);
113128
};
114129

130+
// captureScroll
115131
window.addEventListener('scroll', scrollHandler, false);
116132
window.addEventListener('resize', resizeHandler, false);
117133
},

dist/scrollReveal.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scrollReveal.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ window.scrollReveal = (function (window) {
2828

2929
'use strict';
3030

31+
/**
32+
* RequestAnimationFrame polyfill
33+
* @function
34+
* @private
35+
*/
36+
var requestAnimFrame = (function () {
37+
return window.requestAnimationFrame ||
38+
window.webkitRequestAnimationFrame ||
39+
window.mozRequestAnimationFrame ||
40+
function (callback) {
41+
window.setTimeout(callback, 1000 / 60);
42+
};
43+
}());
44+
3145
function scrollReveal(options) {
3246

3347
this.docElem = window.document.documentElement;
@@ -79,12 +93,13 @@ window.scrollReveal = (function (window) {
7993
self.update(el);
8094
});
8195

82-
var scrollHandler = function () {
96+
var scrollHandler = function (e) {
97+
// No changing, exit
8398
if (!self.scrolled) {
8499
self.scrolled = true;
85-
setTimeout(function () {
86-
self._scrollPage();
87-
}, 60);
100+
requestAnimFrame(function () {
101+
self._scrollPage
102+
});
88103
}
89104
};
90105

@@ -101,6 +116,7 @@ window.scrollReveal = (function (window) {
101116
self.resizeTimeout = setTimeout(delayed, 200);
102117
};
103118

119+
// captureScroll
104120
window.addEventListener('scroll', scrollHandler, false);
105121
window.addEventListener('resize', resizeHandler, false);
106122
},

0 commit comments

Comments
 (0)