Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

Commit

Permalink
Fix #9 issue with wrong sticky position after refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
rgalus committed Apr 11, 2017
1 parent 02f48c3 commit b73391e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class Sticky {
this.version = '1.1.7';

this.vp = this.getViewportSize();
this.scrollTop = this.getScrollTopPosition();
this.body = document.querySelector('body');

this.options = {
Expand All @@ -35,6 +34,12 @@ class Sticky {
stickyContainer: options.stickyContainer || 'body',
};

this.updateScrollTopPosition = this.updateScrollTopPosition.bind(this);

this.updateScrollTopPosition();
window.addEventListener('load', this.updateScrollTopPosition);
window.addEventListener('scroll', this.updateScrollTopPosition);

this.run();
}

Expand Down Expand Up @@ -215,8 +220,6 @@ class Sticky {
* @param {node} element - Element for which event function is fired
*/
onScrollEvents(element) {
this.scrollTop = this.getScrollTopPosition();

if (element.sticky.active) {
this.setPosition(element);
}
Expand Down Expand Up @@ -385,12 +388,12 @@ class Sticky {


/**
* Function that returns scroll position offset from top
* Function that updates window scroll position
* @function
* @return {number}
*/
getScrollTopPosition() {
return (window.pageYOffset || document.scrollTop) - (document.clientTop || 0) || 0;
updateScrollTopPosition() {
this.scrollTop = (window.pageYOffset || document.scrollTop) - (document.clientTop || 0) || 0;
}


Expand Down

0 comments on commit b73391e

Please sign in to comment.