HTML - DOM样式对象flexDirection属性



HTML DOM 样式对象 **flexDirection** 属性设置或返回弹性元素的放置方向。

语法

设置 flexDirection 属性
object.style.flexDirection= "row | row-reverse | column | column-reverse | initial | inherit";
获取 flexDirection 属性
object.style.flexDirection;

属性值

描述
row 这是默认值,它将弹性项目水平显示为一行。
row-reverse 与 row 相同,但顺序相反。
column 它将弹性项目垂直显示为一列。
column-reverse 与 column 相同,但顺序相反。
initial 用于将此属性设置为其默认值。
inherit 用于继承其父元素的属性。

返回值

它返回一个字符串值,表示元素的 flex direction 属性。

HTML DOM 样式对象“flexDirection”属性示例

以下示例说明了 flex direction 属性的不同属性值。

将 Flex Direction 设置为“row-reverse”

以下示例将 flex direction 设置为 row-reverse,默认设置为 row。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        #flex {
            width: 500px;
            height: 500px;
            border: 1px solid black;
            display: flex;
        }
        #flex div {
            height: 80px;
            width: 80px;
        }
    </style>
    <title>
        HTML DOM Style Object flexDirection Property
    </title>
</head>
<body>
    <p>Click to apply flexDirection property.</p>
    <button onclick="fun()">Row-Reverse</button>
    <br>
    <br>
    <div id="flex">
        <div style="background-color: #04af2f;">1</div>
        <div style="background-color: aqua;">2</div>
        <div style="background-color: yellow;">3</div>
        <div style="background-color: whitesmoke">4</div>
        <div style="background-color: black;">5</div>
    </div>
    <script>
        function fun() {
            document.getElementById("flex")
                .style.flexDirection = "row-reverse"
        }
    </script>
</body>
</html>

将 Flex Direction 设置为“column”和“column-reverse”

在此示例中,我们将 flex direction 设置为 column 和 column-reverse。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        #flex {
            width: 500px;
            height: 500px;
            border: 1px solid black;
            display: flex;
        }
        #flex div {
            height: 80px;
            width: 80px;
        }
    </style>
    <title>
        HTML DOM Style Object flexDirection Property
    </title>
</head>
<body>
    <p>Click to apply flexDirection property.</p>
    <button onclick="fun()">Column</button>
    <button onclick="funTwo()">Column-Reverse</button>
    <br>
    <br>
    <div id="flex">
        <div style="background-color: #04af2f;">1</div>
        <div style="background-color: aqua;">2</div>
        <div style="background-color: yellow;">3</div>
        <div style="background-color: whitesmoke">4</div>
        <div style="background-color: black;">5</div>
    </div>
    <script>
        function fun() {
            document.getElementById("flex")
                .style.flexDirection = "column"
        }
        function funTwo() {
            document.getElementById("flex")
                .style.flexDirection = "column-reverse"
        }
    </script>
</body>
</html>

支持的浏览器

属性 Chrome Edge Firefox Safari Opera
flexDirection 是 29 是 12 是 20 是 9 是 12.1
html_dom_style_object_reference.htm
广告
© . All rights reserved.