summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjeffreytse <jeffreytse.mail@gmail.com>2020-08-31 17:21:45 +0800
committerjeffreytse <jeffreytse.mail@gmail.com>2020-08-31 17:21:45 +0800
commit9b5b05463d34f5d8391ae208be72ba2ced75b3f1 (patch)
tree33e85584e40da2a640934f05b1beece0a63ce578
parent690e71acfcb688259f45e9f8f3e5e3ada41a4c0e (diff)
feat: auto collapsed menu
-rw-r--r--_includes/sidebar/article-menu.html47
1 files changed, 45 insertions, 2 deletions
diff --git a/_includes/sidebar/article-menu.html b/_includes/sidebar/article-menu.html
index 96a4968..215e808 100644
--- a/_includes/sidebar/article-menu.html
+++ b/_includes/sidebar/article-menu.html
@@ -36,10 +36,49 @@
// The header element
var header = document.querySelector('header.site-header');
+ function doMenuCollapse(index) {
+ var items = menuContent.firstChild.children;
+ var activeItem = items[index];
+ var beginItem = activeItem
+ var endItem = activeItem
+ var beginIndex = index;
+ var endIndex = index + 1;
+ while (beginIndex >= 0
+ && !items[beginIndex].classList.contains('h-h2')) {
+ beginIndex -= 1;
+ }
+ while (endIndex < items.length
+ && !items[endIndex].classList.contains('h-h2')) {
+ endIndex += 1;
+ }
+ for (var i = 0; i < beginIndex; i++) {
+ item = items[i]
+ if (!item.classList.contains('h-h2')) {
+ item.style.display = 'none';
+ }
+ }
+ for (var i = beginIndex + 1; i < endIndex; i++) {
+ item = items[i]
+ // if (!item.classList.contains('h-h2')) {
+ item.style.display = '';
+ // }
+ }
+ for (var i = endIndex; i < items.length; i++) {
+ item = items[i]
+ if (!item.classList.contains('h-h2')) {
+ item.style.display = 'none';
+ }
+ }
+ }
+
+ // Init menu collapsed
+ doMenuCollapse(-1);
+
// Active the menu item
window.addEventListener('scroll', function (event) {
var lastActive = menuContent.querySelector('.active');
var changed = true;
+ var activeIndex = -1;
for (var i = headings.length - 1; i >= 0; i--) {
var h = headings[i];
var headingRect = h.getBoundingClientRect();
@@ -53,6 +92,7 @@
var curActive = a.parentNode;
if (curActive) {
curActive.classList.add('active');
+ activeIndex = i;
}
if (lastActive == curActive) {
changed = false;
@@ -60,8 +100,11 @@
break;
}
}
- if (lastActive && changed) {
- lastActive.classList.remove('active');
+ if (changed) {
+ if (lastActive) {
+ lastActive.classList.remove('active');
+ }
+ doMenuCollapse(activeIndex);
}
event.preventDefault();
});