HTML - DOM樣式物件overflowY屬性



HTML DOM 樣式物件overflowY屬性決定了如果內容不適合元素框,如何處理內容的頂部和底部邊緣。

語法

設定 overflowY 屬性
object.style.overflowY= "visible | hidden | scroll | auto | initial | inherit";
獲取 overflowY 屬性
object.style.overflowY;

屬性值

描述
visible 這是預設值,其中元素框外的內容不會被裁剪,而是顯示在元素框之外。
hidden 它會裁剪元素框外的內容,只顯示元素框內的內容,其餘內容隱藏。
scroll 它會裁剪內容以適應元素框,併為元素框外的內容新增捲軸。
auto 它會裁剪內容,只有在需要時且內容溢位時才新增捲軸。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的 overflow-y 屬性。

HTML DOM 樣式物件“overflowY”屬性示例

以下示例演示了 overflowY 屬性,用於隱藏和向 div 元素新增捲軸。

隱藏垂直溢位的內容

以下示例使用hidden屬性值隱藏溢位元素框的內容。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object overflowY Property
    </title>
    <style>
        #overflowY {
            border: 2px solid yellow;
            height: 200px;
            width: 200px;
        }
    </style>
</head>
<body>
    <button onclick="funtwo()">Hide</button>
    <br><br><br>
    <div id="overflowY">
        <p>
            CSS is the acronym for "Cascading Style Sheet".
        </p>
        <p>
            It's a style sheet language used for describing
            the presentation of a document written in a
            markup language like HTML.
        </p>
        <p>
            CSS helps the web developers to control
            the layout and other visual aspects of
            the web pages.
        </p>
        <p>
            CSS plays a crucial role in modern web
            development by providing the tools necessary
            to create visually appealing, accessible, and
            responsive websites.
        </p>
    </div>
    <script>
        function funtwo() {
            document.getElementById("overflowY")
                .style.overflowY = "hidden";
        }
    </script>
</body>
</html>

向 div 元素新增捲軸

以下示例使用scroll屬性值向 div 元素新增捲軸。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object overflowY Property
    </title>
    <style>
        #overflowY {
            border: 2px solid yellow;
            height: 200px;
            width: 200px;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Add Scrollbar</button>
    <br><br><br>
    <div id="overflowY">
        <p>
            CSS is the acronym for "Cascading Style Sheet".
        </p>
        <p>
            It's a style sheet language used for describing
            the presentation of a document written in a
            markup language like HTML.
        </p>
        <p>
            CSS helps the web developers to control
            the layout and other visual aspects of
            the web pages.
        </p>
        <p>
            CSS plays a crucial role in modern web
            development by providing the tools necessary
            to create visually appealing, accessible, and
            responsive websites.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("overflowY")
                .style.overflowY = "scroll";
        }
    </script>
</body>
</html>

將 overflow 屬性設定為 auto

以下屬性將 overflowY 屬性設定為auto,這會在 div 元素的右側新增捲軸。

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Style Object overflowY Property
    </title>
    <style>
        #overflowY {
            border: 2px solid yellow;
            height: 200px;
            width: 200px;
        }
    </style>
</head>
<body>
    <button onclick="fun()">Auto</button>
    <br><br><br>
    <div id="overflowY">
        <p>
            CSS is the acronym for "Cascading Style Sheet".
        </p>
        <p>
            It's a style sheet language used for describing
            the presentation of a document written in a
            markup language like HTML.
        </p>
        <p>
            CSS helps the web developers to control
            the layout and other visual aspects of
            the web pages.
        </p>
        <p>
            CSS plays a crucial role in modern web
            development by providing the tools necessary
            to create visually appealing, accessible, and
            responsive websites.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("overflowY")
                .style.overflowY = "auto";
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
overflowY 是 1 是 12 是 3.5 是 3 是 9.5
html_dom_style_object_reference.htm
廣告