简体   繁体   English

创建一个按钮以在样式的行和列之间切换 flex-direction

[英]Create a button to toggle flex-direction between row and column for a style

I am tying to create a button that will toggle a style flex-direction between "row" and "column"我正在创建一个按钮,它将在“行”和“列”之间切换样式弹性方向

The code I tried was based on some other code that I used for toggling the display between "flex" and "none".我尝试的代码基于我用于在“flex”和“none”之间切换显示的其他一些代码。

I can't use Id's because there a multiple non contiguous element.我不能使用 Id,因为有多个非连续元素。

I TRIED THE CODE BELOW我试过下面的代码

 function setDisplay(className, displayValue) { var items = document.getElementsByClassName(className); for (var i=0; i < items.length; i++) { items[i].style.flexDirection = displayValue; } } function showResponsive() { var elements = document.getElementsByClassName("main"); Array.prototype.forEach.call(elements, function(el) { if (el.style.flex-direction === "column") { el.style.flex-direction = "row"; } else { el.style.flex-direction = "column"; } }); }
 .green{ background:green; width:300px; height:300px; } .red{ background:red; width:300px; height:300px; } .black{ background:black; width:300px; height:300px; } .main{ display:flex; flex-direction:column }
 <div><i>Click switch</i></div> <div> <label class="switch"> <input type="checkbox" onclick="showResponsive()" > <span class="slider round"></span> </label> </div> <div class="main"> <div class="green"></div> <div class="red"></div> <div class="black"></div> </div>

The code above is based on the code below which works for changing the style.display between "flex" and "none"上面的代码基于下面的代码,用于在“flex”和“none”之间更改 style.display

function setDisplay(className, displayValue) {
    var items = document.getElementsByClassName(className);
    for (var i=0; i < items.length; i++) {
      items[i].style.flexDirection = displayValue;
    }
}
function showResponsive() {
    var elements = document.getElementsByClassName("main");
    Array.prototype.forEach.call(elements, function(el) {
      if (el.style.flex-direction === "column") {
          el.style.flex-direction = "row";
      } else {
          el.style.flex-direction = "column";
      }
    });
}

Replace your JavaScript code with the following code:将您的 JavaScript 代码替换为以下代码:

function showResponsive () {
    var elements = document.getElementsByClassName("main");
    Array.prototype.forEach.call(elements, function(el) {
      if (el.style.flexDirection === "column") {
          el.style.flexDirection = "row";
      } else {
          el.style.flexDirection = "column";
      }
    });
}

The property names on the style object are in camel-case and not kebab-case.样式对象上的属性名称是驼峰式的,而不是烤肉串的。 So when setting the flex direction use:所以在设置 flex 方向时使用:

el.style.flexDirection = "row";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮时更改 flex-direction - Change the flex-direction on button click 如何在 JavaScript 中应用 display: flex 和 flex-direction: row? - How to apply display: flex and flex-direction: row in JavaScript? 具有 flex-direction column-reverse 的 Flex 容器不会滚动到顶部 - Flex container with flex-direction column-reverse will not scroll to top justify-content: flex-end; 和 flex-direction: 列; 没有一起工作 - justify-content: flex-end; and flex-direction: column; are not working together 在具有flex-direction的flexbox中,ngx-datatable不缩小:行 - Ngx-datatable not shrinking when within flexbox with flex-direction: row 如何正确获取使用flex-direction:column显示的块的实际宽度? - How to get actual width of a block displayed with flex-direction:column properly? 如何制作 flex-direction: column; 适当地。 我的代码不起作用 - How to make flex-direction: column; properly. My code doesn't work CSS - 在 ul 中使用 flex-direction 附加 li 标记:column-reverse 无法正常工作 - CSS - Appending li tag in ul with flex-direction: column-reverse not working correctly 在列弯曲方向内使用行弯曲方向 - use row flex direction within column flex direction 你能在 CSS 和 JavaScript 中动态改变 flexbox 的 flex-direction 吗? - Can you change the flex-direction of flexbox dynamically in CSS and JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM