
- 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
廣告