-
-
Notifications
You must be signed in to change notification settings - Fork 862
Fix buggy header navigation bar #1157
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
Merged
jbubar
merged 2 commits into
hackforla:gh-pages
from
akibrhast:1055-fix-buggy-header-nav
Mar 6, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,91 +1,55 @@ | ||
| <div id="swapButton"> | ||
| <ul class="inline-list"> | ||
| <li> | ||
| <button onclick="burgerSwap()" id="burgerImage">{% include svg/icon-hamburger-nav.svg %}</button> | ||
| <button onclick="burgerSwap()" id="burgerImageX">{% include svg/icon-hamburger-nav-x.svg %}</button> | ||
| <button id="burgerImage" >{% include svg/icon-hamburger-nav.svg %}</button> | ||
| </li> | ||
| </ul> | ||
| </div> | ||
|
|
||
| <script> | ||
| //Retrieve svg string from _includes | ||
| const burgerImage = `{% include svg/icon-hamburger-nav.svg %}` ; | ||
| const burgerImageX = `{% include svg/icon-hamburger-nav-x.svg %}` ; | ||
|
|
||
| // visibleBurger marks the state of the icon. if 'true' the burger icon is showing | ||
| let visibleBurger = true; | ||
|
|
||
| function burgerSwap() { | ||
| // Initialize | ||
| let burgerImage = document.getElementById("burgerImage"); | ||
| let burgerImageX = document.getElementById("burgerImageX"); | ||
| let burgerMenu = document.getElementById("headerNav"); | ||
|
|
||
| //Test to see the current state of the image | ||
| if (visibleBurger == true) { | ||
| // If the burger is showing, swap for the X and show the menu. | ||
| burgerImage.style.display = "none"; | ||
| burgerImageX.style.display = "block"; | ||
| burgerMenu.style.display = "flex"; | ||
|
|
||
| //Change the state marker | ||
| visibleBurger = false; | ||
|
|
||
| } else { | ||
| //If the X and menu is showing, swap for burger and close the menu. | ||
| burgerImage.style.display = "block"; | ||
| burgerImageX.style.display = "none"; | ||
| burgerMenu.style.display = "none"; | ||
| document.querySelector('#burgerImage').addEventListener('click',toggleNavDisplay); | ||
| window.addEventListener('resize', resetNavBarPropsToDefaultState); | ||
|
|
||
| function toggleNavDisplay(){ | ||
| swapIcons(this.firstElementChild.id); | ||
| document.querySelector('#headerNav').style.display == 'flex' ? document.querySelector('#headerNav').style.display = 'none': document.querySelector('#headerNav').style.display = 'flex'; | ||
|
|
||
|
|
||
| //Change the state marker | ||
| visibleBurger = true; | ||
| } | ||
| event.stopPropagation(); | ||
| } | ||
| // When you click anywhere on the page, other than the menu, if the menu is open, this closes it. | ||
| function clickOutsideToClose() { | ||
| if (visibleBurger == false) { | ||
| let eventID = event.target.id; | ||
|
|
||
| // If the click is on the navigation menu, it does nothing. If it is not on the navmenu it runs burgerSwap and closes the menu. | ||
| if (eventID != "headerNav") { | ||
| burgerSwap(); | ||
| event.stopPropagation(); | ||
| } | ||
|
|
||
|
|
||
| function swapIcons(icon_id){ | ||
| //Create dictionary for swap | ||
| const hamburger = { | ||
| 'hamburger-nav': {"el":burgerImage,'opposite':burgerImageX}, | ||
| 'hamburger-nav-x':{'el':burgerImageX,'opposite':burgerImage} | ||
| } | ||
| } | ||
|
|
||
| // If this is mobile, it adds event listeners to close the menus. | ||
| let windowWidth = window.innerWidth; | ||
| if (windowWidth < 768) { | ||
| //Listen for someone clicking a link from the mobile nav. | ||
| document.getElementById("mobileNavLink").addEventListener("click", burgerSwap); | ||
| //remove existing icon | ||
| document.querySelector(`#${icon_id}`).remove(); | ||
|
|
||
| //insert opposite icon | ||
| document.querySelector(`#burgerImage`).insertAdjacentHTML('afterbegin',hamburger[icon_id].opposite); | ||
|
|
||
|
|
||
| //Listen for someone clicking anywhere on the page to close the menus. | ||
| window.addEventListener("click", clickOutsideToClose); | ||
| } | ||
|
|
||
|
|
||
| function resetNavBarPropsToDefaultState(){ | ||
| if(document.body.clientWidth>767){ | ||
| document.querySelector('#headerNav').removeAttribute("style"); | ||
| document.querySelector(`#burgerImage`).firstElementChild.remove(); | ||
| document.querySelector(`#burgerImage`).insertAdjacentHTML('afterbegin',burgerImage); | ||
|
|
||
| // There was bug where if you go from portrait to landscape on a really tall phone, you lose the navigation links. | ||
| // This was caused by hiding the links in portrait and they're still hidden in landscape. | ||
| // This code fixes that bug | ||
| window.addEventListener("orientationchange", handleOrientationChange, true); | ||
|
|
||
| function handleOrientationChange (event) { | ||
| let windowHeight = window.innerHeight; | ||
| let windowWidth = window.innerWidth; | ||
| let burgerMenu = document.getElementById("headerNav"); | ||
| // "if statements" only affect tall phones | ||
| if(windowHeight >= 768) { | ||
| // Should display original nav bar for large phones | ||
| document.getElementById("mobileNavLink").removeEventListener("click", burgerSwap); | ||
| window.removeEventListener("click", clickOutsideToClose); | ||
| burgerMenu.style.display = "block"; | ||
| } else if (windowWidth >= 768) { | ||
| // Reset things to mobile nav bar | ||
| document.getElementById("mobileNavLink").addEventListener("click", burgerSwap); | ||
| window.addEventListener("click", clickOutsideToClose); | ||
| // Trigger burger swap to set things to initial view | ||
| visibleBurger = false; | ||
| burgerSwap(); | ||
| } | ||
| event.stopPropagation(); | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| </script> | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.