Skip to content

Commit a132375

Browse files
committed
Merge pull request jlmakes#46 from pazguille/master
Decouple the scroll event from _scrollPage function using requestAnimationFrame.
2 parents 6c71e63 + 9225c07 commit a132375

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
@@ -31,6 +31,20 @@ window.scrollReveal = (function (window) {
3131
// generator (increments) for the next scroll-reveal-id
3232
var nextId = 1;
3333

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

3650
this.docElem = window.document.documentElement;
@@ -87,12 +101,13 @@ window.scrollReveal = (function (window) {
87101
self.update(el);
88102
});
89103

90-
var scrollHandler = function () {
104+
var scrollHandler = function (e) {
105+
// No changing, exit
91106
if (!self.scrolled) {
92107
self.scrolled = true;
93-
setTimeout(function () {
94-
self._scrollPage();
95-
}, 60);
108+
requestAnimFrame(function () {
109+
self._scrollPage
110+
});
96111
}
97112
};
98113

@@ -109,6 +124,7 @@ window.scrollReveal = (function (window) {
109124
self.resizeTimeout = setTimeout(delayed, 200);
110125
};
111126

127+
// captureScroll
112128
window.addEventListener('scroll', scrollHandler, false);
113129
window.addEventListener('resize', resizeHandler, false);
114130
},

0 commit comments

Comments
 (0)