diff options
author | xengineering <me@xengineering.eu> | 2021-09-05 14:01:13 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2021-09-05 14:01:13 +0200 |
commit | 5e662b856baff0714784a91551ef961cd5214542 (patch) | |
tree | 27b70ee121239d113387301e5f9ee4d4ee854109 /js | |
parent | 437bf22078af95f4576fec452bcbc88c1a9a09ec (diff) | |
download | libweb-5e662b856baff0714784a91551ef961cd5214542.tar libweb-5e662b856baff0714784a91551ef961cd5214542.tar.zst libweb-5e662b856baff0714784a91551ef961cd5214542.zip |
Separate HTML and Javascript
Diffstat (limited to 'js')
-rw-r--r-- | js/xengineering.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/js/xengineering.js b/js/xengineering.js new file mode 100644 index 0000000..9d10026 --- /dev/null +++ b/js/xengineering.js @@ -0,0 +1,50 @@ +// vim: shiftwidth=2 tabstop=2 expandtab + +// define menu state +const MenuState = { + Desktop: "Desktop", + MobileCollapsed: "MobileCollapsed", + MobileExpanded: "MobileExpanded" +} +var state; + +// init menu state and add mediaChange handler +var isMobile = window.matchMedia("(max-width: 767px)"); +mediaChange(isMobile); +isMobile.addListener(mediaChange) + +function mediaChange(isMobile) { + if (isMobile.matches) { + state = MenuState.MobileCollapsed; + handleState(state); + } else { + state = MenuState.Desktop; + handleState(state); + } +} + +function toggleMenu() { + switch (state) { + case MenuState.MobileCollapsed: + state = MenuState.MobileExpanded; + handleState(state); + break; + case MenuState.MobileExpanded: + state = MenuState.MobileCollapsed; + handleState(state); + break; + } +} + +function handleState(state) { + var items = document.getElementsByTagName("a"); + for(var i=0; i<items.length; i++) { + if (items[i].parentNode.tagName == "NAV") { + if (state == MenuState.MobileCollapsed) { + items[i].style.display = "none"; + } else { + items[i].style.display = "block"; + } + } + } +} |