-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
216 additions
and
100 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,49 @@ | ||
export default function navbar () { | ||
// Get all "navbar-burger" elements | ||
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0) | ||
const html = document.getElementsByTagName('html')[0] | ||
const navbar = document.getElementById('navbar') | ||
const htmlStickyClasses = ['has-navbar-fixed-top'] | ||
const navbarStickyClasses = ['is-fixed-top', 'has-shadow'] | ||
|
||
// Check if there are any navbar burgers | ||
if ($navbarBurgers.length > 0) { | ||
// Add a click event on each of them | ||
$navbarBurgers.forEach(el => { | ||
el.addEventListener('click', () => { | ||
// Get the target from the "data-target" attribute | ||
const target = el.dataset.target | ||
const $target = document.getElementById(target) | ||
let lastScrollY = 0 | ||
let state = { | ||
stickyBarEnabled: true, | ||
stickyBarVisible: true | ||
} | ||
|
||
function updateState (stateChanges) { | ||
const newState = { ...state, ...stateChanges } | ||
if (JSON.stringify(newState) !== JSON.stringify(state)) { | ||
state = newState | ||
update() | ||
} | ||
} | ||
|
||
// Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu" | ||
el.classList.toggle('is-active') | ||
$target.classList.toggle('is-active') | ||
}) | ||
}) | ||
function update () { | ||
if (state.stickyBarEnabled && state.stickyBarVisible) { | ||
for (const className of htmlStickyClasses) { | ||
html.classList.add(className) | ||
} | ||
for (const className of navbarStickyClasses) { | ||
navbar.classList.add(className) | ||
} | ||
} else { | ||
for (const className of htmlStickyClasses) { | ||
html.classList.remove(className) | ||
} | ||
for (const className of navbarStickyClasses) { | ||
navbar.classList.remove(className) | ||
} | ||
} | ||
} | ||
|
||
const mobileMq = window.matchMedia('screen and (max-width: 768px)') | ||
mobileMq.addEventListener('change', (e) => { | ||
updateState({ stickyBarEnabled: e.matches }) | ||
}) | ||
|
||
window.addEventListener('scroll', (e) => { | ||
const isScrollingUp = window.scrollY < lastScrollY | ||
updateState({ stickyBarVisible: isScrollingUp }) | ||
lastScrollY = window.scrollY | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters