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
广告
© . All rights reserved.