【发布时间】:2019-12-05 01:31:25
【问题描述】:
我可以在遍历项目以在每个奇数或偶数项目之前和之后添加一个 div 时执行此操作,但我想知道是否有办法使用 JUST CSS flex 来完成此操作。
我想摆脱 .col div 并只使用 CSS。
我需要显示一行水平滚动的项目或水平滚动项目的两行。
一排用于移动屏幕,两排用于任何大于移动设备的屏幕。
这是一支笔
https://codepen.io/modemlooper/pen/rXWaVK
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let html = "";
const device = "tablet";
items.map((item, index) => {
html +=
"tablet" === device && index % 2 === 0 ? '<div class="col">' : "";
html += '<div class="slide"><div>' + item + "</div></div>";
html += "tablet" === device && index % 2 !== 0 ? "</div>" : "";
});
var e = document.createElement("div");
e.className += " box";
e.innerHTML = html;
document.querySelector(".wrap").appendChild(e);
.wrap {
display: flex;
height: 100%;
align-items: center;
}
.box {
display: flex;
background-color: #2c3e50;
min-width: 100%;
overflow-x: auto;
box-sizing: border-box;
}
.box::-webkit-scrollbar {
display: none;
}
.slide {
box-sizing: border-box;
padding: 20px;
}
.slide>div {
min-width: 218px;
height: 178px;
display: flex;
box-sizing: border-box;
}
.slide:nth-child(odd)>div {
background: red;
}
.slide:nth-child(even)>div {
background: blue;
}
.col:nth-child(odd) .slide:nth-child(odd)>div {
background: red;
}
.col:nth-child(even) .slide:nth-child(even)>div {
background: red;
}
.col:nth-child(even) .slide:nth-child(odd)>div {
background: blue;
}
.col:nth-child(odd) .slide:nth-child(even)>div {
background: blue;
}
<div class="wrap"></div>
【问题讨论】:
-
块的
height是静态的吗? -
是的,它们有固定的宽度和高度。