當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML DOM Style overflow屬性用法及代碼示例


DOM 樣式溢出屬性返回並修改 HTML 文檔中元素的溢出 CSS 屬性。

用法

以下是語法 -

  • 返回溢出

object.style.overflow
  • 修改溢出

object.style.overflow = “value”

這裏的價值可以是 -

解釋
scroll它剪輯內容並在必要時添加滾動條。
inherit它從其父元素繼承此屬性值。
initial它將此屬性值設置為其默認值。
auto它剪輯內容並在必要時添加滾動條。
hidden它將內容流隱藏在元素框之外。
visible它不會剪輯內容,內容將流到元素框之外。

示例

讓我們看一個樣式溢出屬性的例子 -

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      color:#000;
      background:lightblue;
      height:100vh;
   }
   p {
      border:2px solid #fff;
      margin:1.5rem auto;
      height:100px;
      overflow:scroll;
   }
   .btn {
      background:#db133a;
      border:none;
      height:2rem;
      border-radius:2px;
      width:40%;
      display:block;
      color:#fff;
      outline:none;
      cursor:pointer;
   }
</style>
</head>
<body>
<h1>DOM Style overflow Property Example</h1>
<p>This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text.
</p>
<button onclick="add()" class="btn">Change overflow</button>
<script>
   function add() {
      document.querySelector('p').style.overflow = "hidden";
   }
</script>
</body>
</html>

輸出

這將產生以下輸出 -

點擊 ”Change overflow” 按鈕更改溢出 CSS 屬性的值scrollhidden

相關用法


注:本文由純淨天空篩選整理自AmitDiwan大神的英文原創作品 HTML DOM Style overflow Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。