Skip to content

Commit

Permalink
Properly call scrollTo callback when already at the target (native)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerek0 committed Mar 23, 2021
1 parent 22c1c0f commit 2e86f14
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
25 changes: 16 additions & 9 deletions dist/locomotive-scroll.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1318,17 +1318,24 @@ var _default$1 = /*#__PURE__*/function (_Core) {
offset = target + offset;
}

if (callback) {
offset = offset.toFixed();
var isTargetReached = function isTargetReached() {
return parseInt(window.pageYOffset) === parseInt(offset);
};

var onScroll = function onScroll() {
if (window.pageYOffset.toFixed() === offset) {
window.removeEventListener('scroll', onScroll);
callback();
}
};
if (callback) {
if (isTargetReached()) {
callback();
return;
} else {
var onScroll = function onScroll() {
if (isTargetReached()) {
window.removeEventListener('scroll', onScroll);
callback();
}
};

window.addEventListener('scroll', onScroll);
window.addEventListener('scroll', onScroll);
}
}

window.scrollTo({
Expand Down
25 changes: 16 additions & 9 deletions dist/locomotive-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -1324,17 +1324,24 @@
offset = target + offset;
}

if (callback) {
offset = offset.toFixed();
var isTargetReached = function isTargetReached() {
return parseInt(window.pageYOffset) === parseInt(offset);
};

var onScroll = function onScroll() {
if (window.pageYOffset.toFixed() === offset) {
window.removeEventListener('scroll', onScroll);
callback();
}
};
if (callback) {
if (isTargetReached()) {
callback();
return;
} else {
var onScroll = function onScroll() {
if (isTargetReached()) {
window.removeEventListener('scroll', onScroll);
callback();
}
};

window.addEventListener('scroll', onScroll);
window.addEventListener('scroll', onScroll);
}
}

window.scrollTo({
Expand Down
2 changes: 1 addition & 1 deletion dist/locomotive-scroll.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/dist/scripts/main.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/scripts/Native.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,18 @@ export default class extends Core {
offset = target + offset;
}

const isTargetReached = () => { return parseInt(window.pageYOffset) === parseInt(offset) }
if (callback) {
offset = offset.toFixed();
if (parseInt(offset) === 0) {
if (isTargetReached()) {
callback();
return;
} else {
} else {
let onScroll = function () {
if (window.pageYOffset.toFixed() === offset) {
if (isTargetReached()) {
window.removeEventListener('scroll', onScroll);
callback();
}
};
};
window.addEventListener('scroll', onScroll);
}
}
Expand Down

0 comments on commit 2e86f14

Please sign in to comment.