核心方法
window.scrollTo()
这个方法会将滚动条滚动到指定的坐标。

(图片来源网络,侵删)
语法:
window.scrollTo(x, y)
window.scrollTo(options)
- 参数
x和y(数字): 滚动到文档的左上角的 X 和 Y 坐标。 - 参数
options(对象): 提供更灵活的控制,可以指定滚动行为。left: 等同于x坐标。top: 等同于y坐标。behavior: 定义动画效果,可以是'auto'(立即滚动,默认) 或'smooth'(平滑滚动)。
示例:
// 立即滚动到页面顶部 (x=0, y=0)
window.scrollTo(0, 0);
// 立即滚动到距离页面顶部 500px 的位置
window.scrollTo(0, 500);
// 平滑滚动到页面顶部
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth'
});
// 平滑滚动到页面右下角
window.scrollTo({
top: document.body.scrollHeight,
left: document.body.scrollWidth,
behavior: 'smooth'
});
window.scrollBy()
这个方法会将滚动条从当前位置滚动指定的距离。
语法:
window.scrollBy(x, y)
window.scrollBy(options)

(图片来源网络,侵删)
- 参数
x和y(数字): 在当前滚动位置上增加的 X 和 Y 坐标。 - 参数
options(对象):left: 等同于x坐标。top: 等同于y坐标。behavior: 同样支持'auto'和'smooth'。
示例:
// 向下滚动 100px (立即)
window.scrollBy(0, 100);
// 向上滚动 100px (立即)
window.scrollBy(0, -100);
// 向右滚动 200px (平滑)
window.scrollBy({
top: 0,
left: 200,
behavior: 'smooth'
});
// 向左滚动 200px (平滑)
window.scrollBy({
top: 0,
left: -200,
behavior: 'smooth'
});
实用场景与代码示例
回到顶部按钮
这是最常见的应用,点击按钮后,页面平滑滚动到顶部。
HTML:
<button id="backToTop">回到顶部</button> <!-- 为了演示,添加一些占位内容 --> <div style="height: 2000px; background: linear-gradient(to bottom, #f0f0f0, #ccc);"> 滚动我来测试... </div>
JavaScript:

(图片来源网络,侵删)
const backToTopButton = document.getElementById('backToTop');
backToTopButton.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
导航到页面内的特定部分 (锚点链接)
使用 scrollTo 可以实现比传统 <a href="#id"> 更灵活的控制,比如添加平滑滚动效果。
HTML:
<nav>
<button onclick="scrollToSection('section1')">去 Section 1</button>
<button onclick="scrollToSection('section2')">去 Section 2</button>
</nav>
<h2>这是页面顶部</h2>
<section id="section1" style="height: 100vh; background-color: lightblue; margin-top: 20px;">
<h2>Section 1</h2>
</section>
<section id="section2" style="height: 100vh; background-color: lightgreen; margin-top: 20px;">
<h2>Section 2</h2>
</section>
JavaScript:
function scrollToSection(sectionId) {
const element = document.getElementById(sectionId);
if (element) {
// element.scrollIntoView({ behavior: 'smooth' }); // 这是另一种更简单的方法
window.scrollTo({
top: element.offsetTop - 50, // 可以减去一个偏移量,比如导航栏高度
behavior: 'smooth'
});
}
}
创建一个自定义的滚动条控制器
创建四个按钮来控制页面的上下左右滚动。
HTML:
<div class="controller"> <button id="scrollUp">上 ↑</button> <button id="scrollDown">下 ↓</button> <button id="scrollLeft">左 ←</button> <button id="scrollRight">右 →</button> </div> <!-- 占位内容 --> <div style="height: 3000px; width: 3000px; background: #f9f9f9;"></div>
CSS:
.controller {
position: fixed;
bottom: 20px;
right: 20px;
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 5px;
z-index: 1000;
}
.controller button {
width: 50px;
height: 50px;
font-size: 20px;
cursor: pointer;
}
JavaScript:
const scrollAmount = 200; // 每次滚动的像素
document.getElementById('scrollUp').addEventListener('click', () => {
window.scrollBy(0, -scrollAmount);
});
document.getElementById('scrollDown').addEventListener('click', () => {
window.scrollBy(0, scrollAmount);
});
document.getElementById('scrollLeft').addEventListener('click', () => {
window.scrollBy(-scrollAmount, 0);
});
document.getElementById('scrollRight').addEventListener('click', () => {
window.scrollBy(scrollAmount, 0);
});
CSS 控制:scroll-behavior
如果你希望整个页面的所有滚动行为都是平滑的,而不是通过 JS 来控制,可以在 CSS 中设置。
CSS:
html {
scroll-behavior: smooth;
}
效果:
设置后,任何用户触发的滚动(如点击锚点链接、按下空格键或方向键)都会自动变成平滑滚动,这不会影响 scrollTo 和 scrollBy 的行为,除非你在 JS 中也指定了 behavior: 'smooth'。
高级技巧:监听滚动事件
有时,你可能需要在用户滚动时执行某些操作,比如显示/隐藏“回到顶部”按钮。
示例:根据滚动位置显示/隐藏按钮
HTML:
<button id="backToTop">回到顶部</button> <div style="height: 2000px;"></div>
CSS:
#backToTop {
position: fixed;
bottom: 20px;
right: 20px;
display: none; /* 默认隐藏 */
padding: 10px 20px;
background-color: #333;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
JavaScript:
const backToTopButton = document.getElementById('backToTop');
// 监听整个文档的滚动事件
window.addEventListener('scroll', () => {
// window.scrollY 获取当前垂直滚动位置
if (window.scrollY > 300) {
// 如果滚动超过300px,显示按钮
backToTopButton.style.display = "block";
} else {
// 否则隐藏按钮
backToTopButton.style.display = "none";
}
});
// 回到顶部功能
backToTopButton.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
| 方法 | 描述 | 示例 |
|---|---|---|
window.scrollTo(x, y) |
滚动到绝对坐标 | window.scrollTo(0, 1000) |
window.scrollTo({top, left, behavior: 'smooth'}) |
平滑滚动到绝对坐标 | window.scrollTo({top: 0, behavior: 'smooth'}) |
window.scrollBy(x, y) |
从当前位置滚动相对距离 | window.scrollBy(0, -100) |
window.scrollBy({top, left, behavior: 'smooth'}) |
平滑滚动相对距离 | window.scrollBy({left: 200, behavior: 'smooth'}) |
element.scrollIntoView() |
滚动到某个可见元素 | document.getElementById('myEl').scrollIntoView({behavior: 'smooth'}) |
window.addEventListener('scroll', ...) |
监听滚动事件 | window.addEventListener('scroll', () => console.log('滚动中...')) |
window.scrollY / window.scrollX |
获取当前滚动位置 | console.log('垂直滚动位置:', window.scrollY) |
html { scroll-behavior: smooth; } |
CSS全局设置平滑滚动 | 在CSS中设置,影响所有用户触发的滚动 |
选择哪种方法取决于你的具体需求,对于简单的交互,scrollTo 和 scrollBy 是最直接的选择,对于复杂的UI组件,你可能需要结合监听滚动事件来实现。
