字母菜单选择功能优化需求:实现点击菜单字母的页面滚动定位
Hey there! Let's get that scroll functionality working smoothly. Here's what we need to fix and improve in your code:
Key Issues in Current Code
- Your heading elements (
h2) don't have matching IDs for the menu links (like#A,#B), so the browser doesn't know where to scroll. - The existing
Removebarfunction only closes the menu—we need to add logic to trigger the scroll when a letter is clicked.
Step-by-Step Fixes
1. Add Matching IDs to Your Headings
First, update each h2 element to have an ID that matches the letter it displays. This lets us target them for scrolling:
<div class="Mainbox" id="box"> <h2 onclick="wordbar()" id="A">A</h2> <ul> <li>Apple</li> <li>Appiv</li> <li>Ananus</li> <li>Ambar</li> </ul> <h2 onclick="wordbar()" id="B">B</h2> <ul> <li>Bejing</li> <li>brothal</li> <li>brother</li> <li>Banana</li> </ul> <!-- Repeat this pattern for C, D, E, F, G, H --> <h2 onclick="wordbar()" id="C">C</h2> <!-- ... rest of your headings ... --> </div>
2. Update JavaScript for Scroll + Menu Close
Replace your existing JavaScript with this updated version. We'll create a dedicated function to handle both closing the menu and scrolling to the right section, plus add optional smooth scrolling for better UX:
function wordbar() { const menu = document.getElementById("Invisible"); if (menu.className === "Hiddenbox") { document.body.style.overflow = "hidden"; menu.style.visibility = "visible"; } else { document.body.style.overflow = "auto"; menu.style.display = "none"; } } // Handles scrolling to the selected letter section and closes the menu function scrollToSection(letter) { // Close the menu first const menu = document.getElementById("Invisible"); document.body.style.overflow = "auto"; menu.style.visibility = "hidden"; // Scroll to the matching heading const targetSection = document.getElementById(letter); if (targetSection) { // Smooth scroll to the top of the heading targetSection.scrollIntoView({ behavior: "smooth", block: "start" }); } } // Optional: Use event delegation to avoid adding onclick to every letter (cleaner!) document.querySelector(".container").addEventListener("click", (e) => { const target = e.target; // Handle letter clicks if (target.classList.contains("click-lines") && target.textContent !== "×") { scrollToSection(target.textContent); } // Handle close button click else if (target.classList.contains("togglebar")) { document.body.style.overflow = "auto"; document.getElementById("Invisible").style.visibility = "hidden"; } });
3. Clean Up Your Menu HTML
If you use the event delegation approach above, you can remove all the onclick attributes and href from your menu items—this keeps your HTML cleaner:
<div class="Hiddenbox" id="Invisible"> <div class="container"> <li class="click-lines">A</li> <li class="click-lines">B</li> <li class="click-lines">C</li> <li class="click-lines">D</li> <li class="click-lines">E</li> <li class="click-lines">F</li> <li class="click-lines">G</li> <li class="click-lines">H</li> <li class="click-lines">I</li> <li class="click-lines">J</li> <!-- ... rest of your letters ... --> <li class="togglebar">×</li> </div> </div>
How It Works
- When you click a letter in the menu, the
scrollToSectionfunction closes the menu and usesscrollIntoView()to smoothly scroll the page to the matchingh2element. - The event delegation approach means you don't have to write repetitive
onclickattributes for every letter—just one event listener on the parent container. - Smooth scrolling is enabled with
behavior: "smooth"to make the transition feel natural.
内容的提问来源于stack exchange,提问作者Shaik Mohammed Huzaifa
相关产品推荐
相关产品推荐

