- HTML 教程
- HTML - 首頁
- HTML - 路線圖
- HTML - 簡介
- HTML - 歷史與演變
- HTML - 編輯器
- HTML - 基本標籤
- HTML - 元素
- HTML - 屬性
- HTML - 標題
- HTML - 段落
- HTML - 字型
- HTML - 塊
- HTML - 樣式表
- HTML - 格式化
- HTML - 引用
- HTML - 註釋
- HTML - 顏色
- HTML - 圖片
- HTML - 影像地圖
- HTML - Iframes
- 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 元素 scrollHeight 屬性
**scrollHeight** 屬性以畫素為單位提供元素內容的總垂直高度。它包括溢位和填充,但不包括邊框、邊距和捲軸。
語法
element.scrollHeight;
返回值
scrollHeight 屬性返回一個整數,該整數包含元素內容的總垂直高度(以畫素為單位)。
HTML DOM 元素“scrollHeight”屬性的示例
以下是一些示例,這些示例顯示瞭如何使用“scrollHeight”屬性來獲取元素內容的總垂直高度(以畫素為單位)。
確定可滾動容器的高度
此示例顯示瞭如何使用 scrollHeight 屬性來確定可滾動容器內內容的總高度。程式碼會在 **<span>** 元素中動態更新顯示的高度。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
#container {
height: 100px;
overflow: auto;
border: 1px solid #ccc;
}
#content {
height: 200px;
background-color: #f0f0f0;
padding: 10px;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>scrollHeight Property</h2>
<div>
<p>Determines the height of content inside a
scrollable container:
</p>
<div id="container">
<div id="content">
This is the content inside the div element.
<br>scroll me!!
</div>
</div>
<p>
Scrollable content height:<span id="r1"></span>
</p>
</div>
<script>
const c1 = document.getElementById('container');
const res = document.getElementById('r1');
res.textContent = c1.scrollHeight;
</script>
</body>
</html>
動態顯示可滾動內容的高度
此示例顯示了一個可滾動框(**<div>**),使用者可以透過在其中單擊來編輯內容。它包含一個按鈕,該按鈕使用 scrollHeight 屬性動態顯示框的可滾動總高度。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
#cont{
height: 30px;
overflow: auto;
border: 1px solid black;
padding: 10px;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>scrollHeight Property</h2>
<div>
<p>Click the button to display the total
scrollable height of the container:
</p>
<p>Try editing the content inside the container</p>
<div id="cont" contenteditable="true">
Click here to edit!!
Content that exceeds the container height.
</div>
<br>
<button onclick="showScrollHeight()">
Show Scrollable Height
</button>
<p>Scrollable content height:
<span id="r"></span>
</p>
</div>
<script>
function showScrollHeight() {
const con = document.getElementById('cont');
const res = document.getElementById('r');
res.textContent = con.scrollHeight + 'px';
}
</script>
</body>
</html>
動態調整 Textarea 高度
此示例顯示了當您鍵入時 scrollHeight 屬性如何調整 Textarea 的高度。Textarea 的高度會更新以適應文字,並且顯示的“當前高度”會根據其內容以畫素為單位顯示實際高度。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
#textcon{
width: 300px;
margin: 20px;
}
textarea {
width: 100%;
min-height: 50px;
border: 1px solid #ccc;
padding: 10px;
box-sizing: border-box;
transition: height 0.3s;
}
</style>
</head>
<body>
<h1>HTML - DOM Element</h1>
<h2>scrollHeight Property</h2>
<h4>Adjusts height based on scrollable content.</h4>
<div id="textcon">
<p>Type in the textarea to see it expand or
compress height dynamically:
</p>
<textarea id="m" oninput="autoAdjustTextarea(this)">
</textarea>
<p>Current height: <span id="ch">50px</span></p>
</div>
<script>
function autoAdjustTextarea(textarea) {
textarea.style.height = 'auto';
textarea.style.height =
textarea.scrollHeight + 'px';
document.getElementById('ch').textContent =
textarea.style.height;
}
</script>
</body>
</html>
支援的瀏覽器
| 屬性 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| scrollHeight | 是 | 是 | 是 | 是 | 是 |
html_dom_element_reference.htm
廣告




