英文:
Smooth scrolling does not work when clicking an anchor link
问题
我在页面上放了一些超链接,用户访问页面时会阅读这些链接。为了使页面更有吸引力,我可以让页面滚动到同一页面内的其他部分。我已经添加了原生JavaScript来实现平滑滚动,但似乎没有起作用。我想知道代码有什么问题。
// 选择所有带有哈希的链接
const links = document.querySelectorAll('a[href^="#"]');
// 为每个链接添加点击事件监听器
links.forEach(link => {
link.addEventListener('click', event => {
// 阻止默认的锚点行为
event.preventDefault();
// 获取目标元素及其顶部偏移量
const targetId = link.hash.slice(1);
const targetElement = document.getElementById(targetId);
if (!targetElement) {
console.error(`无法找到ID为"${targetId}"的元素`);
return;
}
const targetOffset = targetElement.offsetTop;
// 平滑滚动到目标元素
window.scrollTo({
top: targetOffset,
behavior: 'smooth'
});
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>平滑滚动示例</title>
</head>
<body>
<nav>
<ul>
<li><a href="#section1">部分1</a></li>
<li><a href="#section2">部分2</a></li>
<li><a href="#section3">部分3</a></li>
</ul>
</nav>
<main>
<section id="section1">
<h1>部分1</h1>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
</section>
<section id="section2">
<h1>部分2</h1>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
</section>
<section id="section3">
<h1>部分3</h1>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
</section>
</main>
</body>
</html>
英文:
I have a couple of hyperlinks on my page.that users will read when they visit the page.to make it attractive I can make the page scroll toward other section within the same page. i have added vanilla JavaScript to get smooth scroll but it seems not to work . I am wondering why isn't working at all.what is wrong with the code?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Select all links with hashes
const links = document.querySelectorAll('a[href^="#"]');
// Add a click event listener to each link
links.forEach(link => {
link.addEventListener('click', event => {
// Prevent default anchor behavior
event.preventDefault();
// Get the target element and its top offset
const targetId = link.hash.slice(1);
const targetElement = document.getElementById(targetId);
if (!targetElement) {
console.error(`Unable to find element with id "${targetId}"`);
return;
}
const targetOffset = targetElement.offsetTop;
// Scroll smoothly to the target element
window.scrollTo({
top: targetOffset,
behavior: 'smooth'
});
});
});
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Smooth Scroll Example</title>
</head>
<body>
<nav>
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
</ul>
</nav>
<main>
<section id="section1">
<h1>Section 1</h1>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
</section>
<section id="section2">
<h1>Section 2</h1>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
</p>
</section>
<section id="section3">
<h1>Section 3</h1>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna.
Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
</p>
</section>
</main>
</body>
</html>
<!-- end snippet -->
答案1
得分: 1
你的代码可以简化使用委托,在调试之前。
你的 scrollTo 的替代方法是 scrollIntoView。
英文:
Your code works but can be simplified to use delegation before you debug
Alternative for your scrollTo is scrollIntoView
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.querySelector('nav').addEventListener('click', (e) => {
let tgt = e.target.closest('li').querySelector('a'); // make sure the whole LI can be used
if (!tgt) return;
// Prevent default anchor behavior
e.preventDefault();
// Get the target element
const targetId = tgt.hash.slice(1);
const targetElement = document.getElementById(targetId);
if (!targetElement) {
console.error(`Unable to find element with id "${targetId}"`);
return;
}
targetElement.scrollIntoView({ behavior: 'smooth' });
});
<!-- language: lang-html -->
<nav>
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
</ul>
</nav>
<main>
<section id="section1">
<h1>Section 1</h1>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder
text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae,
condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
</section>
<section id="section2">
<h1>Section 2</h1>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder
text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae,
condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting.
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder
text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae,
condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
</section>
<section id="section3">
<h1>Section 3</h1>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder
text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae,
condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting.
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder
text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae,
condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
<p>
Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder
text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae, condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting. Phasellus enim libero, blandit vel sapien vitae,
condimentum ultricies magna. Lorem ipsum is a placeholder text commonly used in design and typesetting.
</p>
</section>
</main>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论