- HTML 教程
- HTML - 首页
- HTML - 路线图
- HTML - 简介
- HTML - 历史与发展
- HTML - 编辑器
- HTML - 基本标签
- HTML - 元素
- HTML - 属性
- HTML - 标题
- HTML - 段落
- HTML - 字体
- HTML - 块级元素
- HTML - 样式表
- HTML - 格式化
- HTML - 引用
- HTML - 注释
- HTML - 颜色
- HTML - 图片
- HTML - 图片地图
- HTML - 内联框架 (Iframe)
- HTML - 短语元素
- HTML - 元标签
- HTML - 类
- HTML - ID
- HTML - 背景
- HTML 表格
- HTML - 表格
- HTML - 表头与标题
- HTML - 表格样式
- HTML - 表格列组 (Colgroup)
- HTML - 嵌套表格
- HTML 列表
- HTML - 列表
- HTML - 无序列表
- HTML - 有序列表
- HTML - 定义列表
- HTML 链接
- HTML - 文本链接
- HTML - 图片链接
- HTML - 邮件链接
- HTML 颜色名称与值
- HTML - 颜色名称
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML 表单
- HTML - 表单
- HTML - 表单属性
- HTML - 表单控件
- HTML - 输入属性
- HTML 多媒体
- HTML - 视频元素
- HTML - 音频元素
- HTML - 嵌入多媒体
- HTML 头部
- HTML - Head 元素
- HTML - 添加 Favicon
- HTML - Javascript
- HTML 布局
- HTML - 布局
- HTML - 布局元素
- HTML - 使用 CSS 进行布局
- HTML - 响应式设计
- HTML - 符号
- HTML - 表情符号
- HTML - 样式指南
- HTML 图形
- HTML - SVG
- HTML - Canvas
- HTML API
- HTML - Geolocation API
- HTML - 拖放 API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web 存储
- HTML - 服务器发送事件
- HTML 其他
- HTML - 文档对象模型 (DOM)
- HTML - MathML
- HTML - 微数据
- HTML - IndexedDB
- HTML - Web 消息传递
- HTML - Web CORS
- HTML - Web RTC
- HTML 演示
- HTML - 音频播放器
- HTML - 视频播放器
- HTML - 网页幻灯片
- HTML 工具
- HTML - Velocity Draw
- HTML - 二维码
- HTML - Modernizer
- HTML - 验证
- HTML - 颜色选择器
- HTML 参考
- HTML - 速查表
- HTML - 标签参考
- HTML - 属性参考
- HTML - 事件参考
- HTML - 字体参考
- HTML - ASCII 码
- ASCII 码表查询
- HTML - 颜色名称
- HTML - 实体
- MIME 媒体类型
- HTML - URL 编码
- 语言 ISO 代码
- HTML - 字符编码
- HTML - 已弃用标签
- HTML 资源
- HTML - 快速指南
- HTML - 有用资源
- HTML - 颜色代码生成器
- HTML - 在线编辑器
HTML - DOM 元素 scrollIntoView() 方法
scrollIntoView 方法确保网页或可滚动容器上的特定元素通过调整滚动位置而可见。
语法
element.scrollIntoView(Options);
参数
| 参数 | 描述 |
|---|---|
| 选项 | 可用于自定义滚动行为的对象,包括平滑滚动、垂直和水平对齐的选项。 |
返回值
scrollIntoView() 方法不返回值。
HTML DOM 元素 'scrollIntoView()' 方法示例
以下是 scrollIntoView() 方法的一些示例,展示了如何在 HTML DOM 中使用它将元素滚动到网页或可滚动容器中的视图中。
使用自定义选项滚动到元素
此示例展示了如何使用 scrollIntoView() 方法将元素滚动到网页或可滚动容器中的视图中。以下代码包含一个具有固定高度和垂直滚动的<div> 元素;单击按钮时,它会将元素滚动到视图中。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.con {
height: 200px;
overflow-y: scroll;
}
.item {
height: 200px;
margin-bottom: 20px;
background-color: lightblue;
}
</style>
</head>
<body>
<h1>HTML DOM - Element</h1>
<h2>scrollIntoView() Method</h2>
<p>Scroll elements to view item3!!.</p>
<div class="con">
<div class="item" id="item1">Item 1</div>
<div class="item" id="item2">Item 2</div>
<div class="item" id="item3">Item 3</div>
<div class="item" id="item4">Item 4</div>
</div>
<button onclick="scrollToItem('item3')">
Scroll to Item 3
</button>
<script>
function scrollToItem(id) {
const its = document.getElementById(id);
its.scrollIntoView
({ behavior: 'smooth', block: 'center' });
}
</script>
</body>
</html>
滚动到页面底部
此示例展示了如何使用 scrollIntoView() 方法将元素滚动到网页或可滚动容器中的视图中。它包含一个按钮,单击后会将页面滚动到底部。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Scroll to Bottom Example</title>
</head>
<body>
<h1>HTML DOM - Element</h1>
<h2>scrollIntoView() Method</h2>
<h1>Scroll to Bottom Example</h1>
<p>Click the button below to scroll smoothly
to the bottom of the page:
</p>
<!-- Button to trigger scrolling -->
<button onclick="scrollToBottom()">
Scroll to Bottom
</button>
<div style="height: 2000px;">
Scroll up to see the button
</div>
<script>
// Function to scroll to the bottom
function scrollToBottom() {
const be = document.body;
be.scrollIntoView
({behavior:'smooth',block:'end'});
}
</script>
</body>
</html>
在容器内水平滚动
此示例展示了 scrollIntoView() 方法的使用,将元素滚动到网页或可滚动容器中的视图中。以下代码允许在容器内水平滚动。单击按钮以滚动到第三个项目。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.con {
width: 300px;
white-space: nowrap;
overflow-x: scroll;
}
.item {
display: inline-block;
width: 250px;
height: 150px;
margin-right: 20px;
background-color: #f0b0ff;
}
</style>
</head>
<body>
<h1>HTML DOM - Element</h1>
<h2>scrollIntoView() Method</h2>
<p>Click Scroll elements to view item3!!.</p>
<div class="con" id="con">
<div class="item" id="item1">Item 1</div>
<div class="item" id="item2">Item 2</div>
<div class="item" id="item3">Item 3</div>
<div class="item" id="item4">Item 4</div>
<div class="item" id="item5">Item 5</div>
</div>
<button onclick="scrollToItem('item3')">
Scroll to Item 3
</button>
<script>
function scrollToItem(itemId) {
const its = document.getElementById(itemId);
its.scrollIntoView
({behavior:'smooth',inline:'end',block:'nearest' });
}
</script>
</body>
</html>
支持的浏览器
| 方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| scrollIntoView() | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
广告




