1 回答

TA貢獻1813條經(jīng)驗 獲得超2個贊
檢查是否展開相應的組。我假設在最終的解決方案中,不同的菜單中不會有相同的鏈接。expandEntryOnLoad
對于滾動:搜索“滾動到元素”,您應該能夠?qū)崿F(xiàn)它。:)
提示:嘗試在問題中創(chuàng)建更簡約、可重現(xiàn)且可運行的代碼片段。這樣,人們就更愿意幫助你,因為它更省時。檢查一下:我被告知要用“堆棧片段”創(chuàng)建一個“可運行”的例子,我該怎么做?
// Move this method to "tree". I just put it at the top so it's easy to find.
function expandEntryOnLoad() {
// Just for testing. Use the target below in production.
let target = "/docs/people.html";
// let target = window.location.pathname;
// Find first "a" with the target as href
let a = [...document.querySelectorAll(".toc a")].find(a => a.pathname === target);
// Expand all tree group parents.
if (a) {
let parent = a;
while (parent = parent.parentElement.closest(".treegroup")) parent.classList.add("is-expanded")
}
}
// Find parent with given selector
function parent_by_selector(elem, cls, stop_selector = 'body') {
// NOTE: Simplified since there is already a function for that.
return elem.closest("." + cls)
};
// Find next sibling of particular class
function nextByClass(elem, cls) {
while (elem = elem.nextElementSibling) {
if (hasClass(elem, cls)) {
return elem;
}
}
return null;
}
// Find previous sibling of particular class
function previousByClass(elem, cls) {
while (elem = elem.previousElementSibling) {
if (hasClass(elem, cls)) {
return elem;
}
}
return null;
}
// Sibling class found?
function hasClass(elem, cls) {
// NOTE: simplified, since there is an easier way to check this. You are already using it at several places.
return elem.classList.contains(cls);
}
"use strict";
function tree() {
let menu = document.querySelector('.toc');
let elements = menu.getElementsByClassName("treeitem");
let sibling = null;
let expanded = false;
eventListeners();
function eventListeners() {
// Listen for click
Array.from(elements).forEach(function(element) {
element.addEventListener('click', function(ev) {
let e = null;
ev.target.classList.contains("treeitem") ? e = ev.target : e = parent_by_selector(ev.target, "treeitem");
sibling = nextByClass(e, "treegroup")
sibling.classList.contains('is-expanded') ? expanded = true : expanded = false;
if (expanded) {
e.classList.remove("is-expanded");
sibling.classList.remove("is-expanded");
} else {
e.classList.add("is-expanded");
sibling.classList.add("is-expanded");
}
}, false);
});
}
expandEntryOnLoad();
};
// WAIT TILL DOCUMENT HAS LOADED BEFORE INITIATING FUNCTIONS
document.addEventListener('DOMContentLoaded', tree());
/* Collapsable */
ul.toc>ul .treegroup:not(.is-expanded) {
display: none;
}
/* Chevron */
.chevron {
height: 0.5rem;
width: 0.5rem;
}
.tree-expander {
position: relative;
cursor: pointer;
user-select: none;
display: block;
padding-left: 0px;
padding-top: 0px;
padding-bottom: 0px;
}
.tree-indicator {
display: inline-block;
position: absolute;
text-align: center;
line-height: 16px;
top: 6px;
left: -1.5em;
color: #5e5e5e;
font-size: 0.6rem;
transition: transform 0.15s ease-in-out;
transform: rotate(0deg);
}
.treeitem.is-expanded>.tree-expander>.tree-indicator {
transform: rotate(90deg);
}
<ul class="toc is-vertically-scrollable has-flex-grow has-flex-shrink">
<ul class="treegroup">
<li class="none"><a href="/docs/level1.html">Level 1</a></li>
<li class="none"><a href="/docs/level2.html">Level 2</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 3</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/index.html">Home</a></li>
<li class="none"><a href="/docs/about.html">About</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 3.2</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/staff.html">Staff</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>People</span>
</li>
</ul>
</ul>
<li class="none"><a href="/docs/level4.html">Level 4</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 5</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/blog.html">Blog</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 5.2</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/data-integration-and-etl/branches-and-loops-local.html">Branches & Loops and everthing else in the world that is in between</a></li>
<li class="none"><a href="/docs/people.html">People</a></li>
</ul>
</ul>
<li class="none"><a href="/docs/level4.html">Level 6</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 7</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/blog.html">Blog</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 5.2</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/data-integration-and-etl/branches-and-loops-local.html">Branches & Loops</a></li>
<li class="none"><a href="/docs/people.html">People</a></li>
</ul>
</ul>
<li class="none"><a href="/docs/level4.html">Level 8</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 9</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/blog.html">Blog</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 5.2</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/data-integration-and-etl/branches-and-loops-local.html">Branches & Loops</a></li>
<li class="none"><a href="/docs/people.html">People</a></li>
</ul>
</ul>
<li class="none"><a href="/docs/level4.html">Level 10</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 11</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/blog.html">Blog</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 5.2</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/data-integration-and-etl/branches-and-loops-local.html">Branches & Loops</a></li>
<li class="none"><a href="/docs/people.html">People</a></li>
</ul>
</ul>
<li class="none"><a href="/docs/level4.html">Level 12</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 13</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/blog.html">Blog</a></li>
<li class="treeitem"><span class="tree-expander"><span class="tree-indicator"><img src="/../docs/assets/images/chevron.png" class="chevron"></span>Level 5.2</span>
</li>
<ul class="treegroup">
<li class="none"><a href="/docs/data-integration-and-etl/branches-and-loops-local.html">Branches & Loops</a></li>
<li class="none"><a href="/docs/people.html">People</a></li>
</ul>
</ul>
</ul>
</ul>
添加回答
舉報