嵌套有序列表(OL)项重叠问题的排查与解决
嵌套有序列表目录重叠问题解决
问题现象
实现嵌套有序列表作为页面目录时,出现主列表项(1、2、3等)被挤在一处,子列表项(1.1、1.1.1等)布局正常但与主项重叠的问题,重叠区域的链接难以点击。不过基于IntersectionObserver的目录高亮当前标题功能、链接跳转功能均正常。
相关代码
JavaScript代码
function isElementInViewport(el) { var rect = el.getBoundingClientRect(); return ( rect.top >= -1 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth) ); } const handler = (entries) => { // Get all the links from the side menu const allLinks = document.querySelectorAll("ol li a"); // Get all the sections we want to track const allSections = document.querySelectorAll("h2"); // Get all entries that have just come into the viewport const allEntries = new Set( entries .filter((entry) => entry.isIntersecting == true) .map((entry) => entry.target) ); let currentSection; // Look through all sections for (let i = 0; i < allSections.length; i++) { // Get the current section currentSection = allSections[i]; // If the section is in the viewport or it has just intersected, set it as active if (isElementInViewport(currentSection) || allEntries.has(currentSection)) { // Set all links as unactive allLinks.forEach((link) => link.classList.remove("active")); // Set current link to active document .querySelector(`a[href="#${currentSection.id}"]`) .classList.add("active"); // Exit loop after setting first active link break; } } }; // Create a new observer with the handler const observer = new IntersectionObserver(handler); // Observe all sections that have an `id` applied document.querySelectorAll("h2").forEach((section) => { observer.observe(section); });
CSS代码
li { padding: 10px 10px 10px 10px; line-height: 16px; } a { color:#000; } #toccontain { position:fixed; left:0px; top:0px; width:300px;} section { display: flex;} ol { position: fixed; width: 10rem; counter-reset: item; list-style-position: outside;} ol li { display: block; clear: left; list-style-position: outside; list-style-type: lower-alpha;} ol li:before { content: counters(item, ".") " "; counter-increment: item;} a:hover { color: green;} .active { color: green;}
HTML代码
<section> <div id="toccontain"> <center>Contents</center> <ol> <li><a href="#intro" style="text-decoration: none;">Introduction</a></li> <li><a href="#debut" style="text-decoration: none;">Debut</a> <ol> <li><a href="#2-1" style="text-decoration: none;">The Targets</a> <ol> <li><a href="#2-1-1" style="text-decoration: none;">Simone Silvestri</a></li> <li><a href="#2-1-2" style="text-decoration: none;">Sean Flynn</a></li> <li><a href="#2-1-3" style="text-decoration: none;">Jett Baili</a></li> </ol> </li> <li><a href="#2-2" style="text-decoration: none;">The Crime</a> <ol> <li><a href="#2-2-1" style="text-decoration: none;">Conditions</a></li> <li><a href="#2-2-2" style="text-decoration: none;">Discovery</a></li> </ol> </li> <li><a href="#2-3" style="text-decoration: none;">Investigation</a></li> </ol> </li> <li><a href="#3" style="text-decoration: none;">GFGJGHJ</a> <ol> <li><a href="#3-1" style="text-decoration: none;">JHKJHK</a></li> <li><a href="#3-2" style="text-decoration: none;">GGR</a></li> <li><a href="#3-3" style="text-decoration: none;">IGRGn</a></li> </ol> </li> </ol> </div> </section>
尝试过的无效方案
- 将ol元素的display属性改为inline/inline-block
- 将嵌套的ol拆分为多个独立列表
最终解决方案
移除CSS代码中ol选择器内的position: fixed属性,即可修复列表项重叠问题。
内容的提问来源于stack exchange,提问作者babbage
相关产品推荐
相关产品推荐

