1 回答
TA贡献1813条经验 获得超2个赞
检查是否展开相应的组。我假设在最终的解决方案中,不同的菜单中不会有相同的链接。expandEntryOnLoad
对于滚动:搜索“滚动到元素”,您应该能够实现它。:)
提示:尝试在问题中创建更简约、可重现且可运行的代码片段。这样,人们就更愿意帮助你,因为它更省时。检查一下:我被告知要用“堆栈片段”创建一个“可运行”的例子,我该怎么做?
// 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>
添加回答
举报
