[英]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.