如何使用JavaScript設定彈性專案的排列方向?


在本教程中,我們將學習如何使用JavaScript設定彈性專案的排列方向。

彈性專案方向可以使用CSS的`flex-direction`屬性來設定。它定義了彈性專案如何在彈性容器中排列。其預設值為“row”,但也可以取其他值,例如“column”、“row-reverse”等。

要使用JavaScript設定彈性專案的排列方向,有多種方法,在本教程中,我們將討論其中兩種:

  • 使用`style.flexDirection`屬性

  • 使用`style.setProperty`方法

使用`style.flexDirection`屬性

可以使用元素的`style.flexDirection`屬性來設定彈性專案的排列方向。元素物件包含此屬性,因此,首先使用`document.getElementById()`方法訪問元素物件,然後使用`style.flexDirection`屬性來使用JavaScript設定彈性專案的排列方向。

語法

const element = document.getElementById('id')

element.style.flexDirection = row | column | row-reverse | column-reverse | inherit | initial

在上述語法中,我們使用`document.getElementById()`方法訪問id為“id”的HTML元素的元素物件,並設定其`style.flexDirection`屬性。

引數

  • row - 彈性專案水平排列成一行(預設值)。

  • column - 彈性專案垂直排列成一列。

  • row-reverse - 彈性專案水平排列成一行,順序相反。

  • column-reverse - 彈性專案垂直排列成一列,順序相反。

  • inherit - `flex-direction`屬性繼承其父元素的屬性。

  • initial - `flex-direction`屬性設定為預設值。

示例1

在下面的示例中,我們使用`flex-direction`屬性來使用JavaScript設定彈性專案的排列方向。您可以嘗試執行以下程式碼,學習如何實現`flexDirection`屬性來設定彈性專案的排列方向:

<!DOCTYPE html> <html> <head> <style> #box { border: 1px solid #000000; width: 450px; height: 150px; display: flex; flex-direction: column; } #box div { flex-grow: 0; flex-shrink: 1; flex-basis: 20px; } </style> </head> <body> <div id="box"> <div style="background-color:orange;">DIV1</div> <div style="background-color:blue;">DIV2</div> <div style="background-color:yellow;" id="myID">DIV3</div> </div> <p>Using flexDirection property</p> <button onclick="display()">Set</button> <script> function display() { document.getElementById("box").style.flexDirection = "row-reverse"; } </script> </body> </html>

示例2

在下面的示例中,我們使用了`style.flexDirection`屬性來使用JavaScript設定彈性專案的排列方向。我們使用了一個“設定彈性方向”按鈕的點選事件來執行`setFlexDirection()`函式,該函式設定多個元素的`flex-direction`屬性。

<html> <head> <style> .flexible-item { display: flex; border: 1px solid black; padding: 10px; margin: 5px 0px; background-color: #f0f0f0; } .flex-item { padding: 10px; border: 1px solid black; background-color: aliceblue; margin: 5px; } </style> </head> <body> <h2> Set the direction of the flexible items using <i> style.flexDirection property </i> with JavaScript </h2> <button onclick="setFlexDirection()"> Set Flex Direction </button> <br /><br /> <div> <div> <b> style.flexDirection = 'row' </b> </div> <div id="element1" class="flexible-item"> <div class="flex-item"> 1 </div> <div class="flex-item"> 2 </div> <div class="flex-item"> 3 </div> </div> <div><b> style.flexDirection = 'column' </b></div> <div id="element2" class="flexible-item"> <div class="flex-item">1</div> <div class="flex-item">2</div> <div class="flex-item">3</div> </div> <div><b> style.flexDirection = 'row-reverse' </b></div> <div id="element3" class="flexible-item"> <div class="flex-item">1</div> <div class="flex-item">2</div> <div class="flex-item">3</div> </div> <div><b> style.flexDirection = 'column-reverse' </b></div> <div id="element4" class="flexible-item"> <div class="flex-item">1</div> <div class="flex-item">2</div> <div class="flex-item">3</div> </div> </div> <script> // 'Set Flex Direction' button click event handler function function setFlexDirection() { const element1 = document.getElementById('element1') const element2 = document.getElementById('element2') const element3 = document.getElementById('element3') const element4 = document.getElementById('element4') // Set the flex-direction property using the style.flexDirection property element1.style.flexDirection = 'row' element2.style.flexDirection = 'column' element3.style.flexDirection = 'row-reverse' element4.style.flexDirection = 'column-reverse' } </script> </body> </html>

使用`style.setProperty`方法

可以使用`style.setProperty`方法在JavaScript中設定元素的任何新屬性或現有屬性。要使用`style.setProperty`方法設定彈性專案的排列方向,我們需要使用`document.getElementById()`方法獲取元素物件,然後就可以輕鬆設定`flex-direction`屬性。在`style.setProperty`方法的屬性名稱引數中,它應該是“flex-direction”,值和優先順序將根據使用者而定。

語法

const element = document.getElementById('id')

element.style.setProperty(property_name, value, priority)

在上述語法中,我們使用`document.getElementById()`方法訪問id為“id”的HTML元素的元素物件,然後使用`style.setProperty`方法。

引數

  • property_name - 要設定的屬性的名稱。

  • value - 屬性的新值。

  • priority - 屬性值的優先順序(可選)。

示例

在下面的示例中,我們使用了`style.setProperty`方法來使用JavaScript設定彈性專案的排列方向。我們使用了一個輸入欄位來獲取使用者對彈性專案排列方向的輸入。一個“設定彈性方向”按鈕與一個點選事件相關聯,該事件執行`setFlexDirection()`函式,該函式設定彈性專案的排列方向。

<html> <head> <style> .flexible-item { display: flex; border: 1px solid black; padding: 10px; margin: 5px 0px; background-color: #f0f0f0; } .flex-item { padding: 10px; border: 1px solid black; background-color: aliceblue; margin: 5px; } </style> </head> <body> <h2> Set the direction of the flexible items using <i> style.setProperty method </i> with JavaScript </h2> <h4>Enter the direction (row, column, row-reverse, column-reverse)of the flexible items:</h4> <input id="flex-direction" type="text" name="flex-direction" value="column"> <button onclick="setFlexDirection()"> Set Flex Direction </button> <br /><br /> <div id="root" class="flexible-item"> <div class="flex-item">1</div> <div class="flex-item">2</div> <div class="flex-item">3</div> </div> <script> // 'Set Flex Direction' button click event handler function function setFlexDirection() { const root = document.getElementById('root') // input field's value const flex_direction = document.getElementById('flex-direction').value // Set the flex-direction property using the style.setProperty method root.style.setProperty('flex-direction', flex_direction) } </script> </body> </html>

在本教程中,我們討論了兩種使用JavaScript設定彈性專案排列方向的方法。第一種方法是使用`style.flexDirection`屬性,另一種方法是使用`style.setProperty`方法。

更新於:2022年11月9日

900 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.