Skip to content

Commit

Permalink
Revert "remove async behaviour of init. fixes #338" (#345)
Browse files Browse the repository at this point in the history
Revert "remove async behaviour of init. fixes #338"
  • Loading branch information
WickyNilliams authored Sep 18, 2019
2 parents bd29047 + e0530f3 commit 50bdaff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cypress/integration/headroom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe("Headroom", function() {
win.hr = new win.Headroom(win.document.querySelector("header"), options);
win.hr.init();
});
cy.wait(200);
};

beforeEach(() => {
Expand Down Expand Up @@ -261,6 +262,7 @@ describe("Headroom", function() {
bottom: false
});
});
cy.wait(20).then(() => {});

cy.scrollTo(0, 0);
cy.should(() => {
Expand Down
22 changes: 16 additions & 6 deletions src/Headroom.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ Headroom.prototype = {
if (Headroom.cutsTheMustard && !this.initialised) {
this.addClass("initial");
this.initialised = true;
this.scrollTracker = trackScroll(this.scroller, this.update.bind(this));

// defer event registration to handle browser
// potentially restoring previous scroll position
setTimeout(
function(self) {
self.scrollTracker = trackScroll(
self.scroller,
self.update.bind(self)
);
},
100,
this
);
}

return this;
Expand All @@ -45,11 +57,9 @@ Headroom.prototype = {
* @public
*/
destroy: function() {
if (this.initialised) {
this.initialised = false;
Object.keys(this.classes).forEach(this.removeClass, this);
this.scrollTracker.destroy();
}
this.initialised = false;
Object.keys(this.classes).forEach(this.removeClass, this);
this.scrollTracker.destroy();
},

/**
Expand Down

0 comments on commit 50bdaff

Please sign in to comment.