Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make scroll and wheel events passive #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Stickyfill",
"version": "1.1.4",
"version": "1.1.5",
"homepage": "http://wd.dizaina.net/en/scripts/stickyfill/",
"authors": [
"Oleg Korsunsky <wd@dizaina.net>"
Expand Down
16 changes: 8 additions & 8 deletions dist/stickyfill.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* Stickyfill -- `position: sticky` polyfill
* v. 1.1.4 | https://github.com/wilddeer/stickyfill
* v. 1.1.5 | https://github.com/wilddeer/stickyfill
* Copyright Oleg Korsunsky | http://wd.dizaina.net/
*
* MIT License
Expand Down Expand Up @@ -74,7 +74,7 @@
rebuild();
return;
}

if (win.pageYOffset != scroll.top) {
updateScrollPos();
recalcAllPos();
Expand Down Expand Up @@ -274,7 +274,7 @@
},
nodeOffset = getElementOffset(node),
parentOffset = getElementOffset(parentNode),

parent = {
node: parentNode,
css: {
Expand Down Expand Up @@ -371,8 +371,8 @@
updateScrollPos();
initAll();

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a heads up that you need to add feature detection for passive events support before applying it to the event listener. Older browsers that don't support it will understand third object argument as truthy capture option.
Here's the snippet you can use borrowed from Modernizr:

var passiveOption = undefined;
try {
  var opts = Object.defineProperty({}, 'passive', {
    get: function() {
      passiveOption = { passive: true };
    }
  });
  window.addEventListener('test', null, opts);
} catch (e) {}
win.addEventListener('scroll', onScroll, passiveOption);
win.addEventListener('wheel', onWheel, passiveOption);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krzksz thanks for the input! I'll test it out on 18F/stickyfill and see if it helps with older browsers!

win.addEventListener('scroll', onScroll);
win.addEventListener('wheel', onWheel);
win.addEventListener('scroll', onScroll, { passive: true });
win.addEventListener('wheel', onWheel, { passive: true });

//watch for width changes
win.addEventListener('resize', rebuild);
Expand All @@ -390,11 +390,11 @@
if (!initialized) return;

deinitAll();

for (var i = watchArray.length - 1; i >= 0; i--) {
watchArray[i] = getElementParams(watchArray[i].node);
}

initAll();
}

Expand All @@ -412,7 +412,7 @@

function stop() {
pause();
deinitAll();
deinitAll();
}

function kill() {
Expand Down
4 changes: 2 additions & 2 deletions dist/stickyfill.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Stickyfill",
"version": "1.1.4",
"version": "1.1.5",
"license": "MIT",
"homepage": "https://github.com/wilddeer/stickyfill",
"repository": {
Expand Down
14 changes: 7 additions & 7 deletions src/stickyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
rebuild();
return;
}

if (win.pageYOffset != scroll.top) {
updateScrollPos();
recalcAllPos();
Expand Down Expand Up @@ -267,7 +267,7 @@
},
nodeOffset = getElementOffset(node),
parentOffset = getElementOffset(parentNode),

parent = {
node: parentNode,
css: {
Expand Down Expand Up @@ -364,8 +364,8 @@
updateScrollPos();
initAll();

win.addEventListener('scroll', onScroll);
win.addEventListener('wheel', onWheel);
win.addEventListener('scroll', onScroll, { passive: true });
win.addEventListener('wheel', onWheel, { passive: true });

//watch for width changes
win.addEventListener('resize', rebuild);
Expand All @@ -383,11 +383,11 @@
if (!initialized) return;

deinitAll();

for (var i = watchArray.length - 1; i >= 0; i--) {
watchArray[i] = getElementParams(watchArray[i].node);
}

initAll();
}

Expand All @@ -405,7 +405,7 @@

function stop() {
pause();
deinitAll();
deinitAll();
}

function kill() {
Expand Down