scroll panel to bring children into view when expanding nav item
authorDan Allen <dan@opendevise.com>
Wed, 30 Dec 2020 10:16:06 +0000 (03:16 -0700)
committerDan Allen <dan@opendevise.com>
Mon, 4 Jan 2021 09:59:07 +0000 (09:59 +0000)
src/js/01-nav.js

index 466dd7f..95f5163 100644 (file)
   }
 
   function toggleActive () {
-    this.classList.toggle('is-active')
+    if (this.classList.toggle('is-active')) {
+      var padding = parseFloat(window.getComputedStyle(this).marginTop)
+      var rect = this.getBoundingClientRect()
+      var menuPanelRect = menuPanel.getBoundingClientRect()
+      var overflowY = (rect.bottom - menuPanelRect.top - menuPanelRect.height + padding).toFixed()
+      if (overflowY > 0) menuPanel.scrollTop += Math.min((rect.top - menuPanelRect.top - padding).toFixed(), overflowY)
+    }
   }
 
   function showNav (e) {