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

[Navbar] Fix navbar hide on scroll behavior, add explicit hide offset #91

Merged
merged 2 commits into from
Oct 27, 2019
Merged
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
28 changes: 21 additions & 7 deletions src/plugins/navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Navbar extends Plugin {
stickyOffset: element.hasAttribute('data-sticky-offset') ? element.getAttribute('data-sticky-offset') : 0,
hideOnScroll: element.hasAttribute('data-hide-on-scroll') ? true : false,
tolerance: element.hasAttribute('data-tolerance') ? element.getAttribute('data-tolerance') : 0,
hideOffset: element.hasAttribute('data-hide-offset') ? element.getAttribute('data-hide-offset') : 0,
shadow: element.hasAttribute('data-sticky-shadow') ? true : false
});
}
Expand All @@ -42,6 +43,7 @@ class Navbar extends Plugin {
stickyOffset: 0,
hideOnScroll: false,
tolerance: 0,
hideOffset: 0,
shadow: false
};
}
Expand Down Expand Up @@ -81,7 +83,7 @@ class Navbar extends Plugin {
* Should this navbar stick to the top of the page?
* @type {boolean}
*/
this.sticky = this.option('sticky');
this.sticky = !!this.option('sticky');

/**
* The offset in pixels before the navbar will stick to the top of the page
Expand All @@ -93,19 +95,25 @@ class Navbar extends Plugin {
* Should the navbar hide when scrolling? Note: this just applies a 'is-hidden-scroll' class.
* @type {boolean}
*/
this.hideOnScroll = this.option('hideOnScroll');
this.hideOnScroll = !!this.option('hideOnScroll');

/**
* The amount of tolerance required before checking the navbar should hide/show
* @type {number}
*/
this.tolerance = this.option('tolerance');
this.tolerance = parseInt(this.option('tolerance'));

/**
* Add a shadow when the navbar is sticky?
* @type {boolean}
*/
this.shadow = this.option('shadow');
this.shadow = !!this.option('shadow');

/**
* The offset in pixels before the navbar will be hidden, defaults to the height of the navbar
* @type {number}
*/
this.hideOffset = parseInt(this.option('hideOffset', Math.max(this.element.scrollHeight, this.stickyOffset)));

/**
* The last scroll Y known, this is used to calculate scroll direction
Expand Down Expand Up @@ -176,10 +184,16 @@ class Navbar extends Plugin {
let scrollDirection = this.calculateScrollDirection(scrollY, this.lastScrollY);
let triggeredTolerance = this.difference(scrollY, this.lastScrollY) >= this.tolerance;

if(triggeredTolerance) {
if(scrollDirection === 'down') {
if (scrollDirection === 'down') {
// only hide the navbar at the top if we reach a certain offset so the hiding is more smooth
let isBeyondTopOffset = scrollY > this.hideOffset;
if (triggeredTolerance && isBeyondTopOffset) {
this.element.classList.add('is-hidden-scroll');
} else {
}
} else {
// if scrolling up to the very top where the navbar would be by default always show it
let isAtVeryTop = scrollY < this.hideOffset;
if (triggeredTolerance || isAtVeryTop) {
this.element.classList.remove('is-hidden-scroll');
}
}
Expand Down