用外部元素中断流
Disrupt flow with outer element
我有一个 flexbox,其元素显示如下:
问题是:有没有办法从 flexbox 外部中断流程 ,以便被阻塞的元素移动到下一个位置,如:
flexbox 是某种排序框,所以我无法在其中放置任何其他元素然后进行排序。通常列是等高的,但有时我需要第一列短一列。
我试过以各种组合方式制作方框 flow: left
和 display: inline-block
,但我无法完成。
.container {
width: 120px;
height: 180px;
margin: 40px 0 0 100px;
}
.flexbox {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
width: 120px;
height: 180px;
background: pink;
}
.inbox {
width: 50px;
height: 50px;
margin: 5px;
background: purple;
color: #fff;
margin-left: auto;
}
.box {
float: left;
width: 110px;
height: 60px;
margin: -60px 0 0 -60px;
background: orange;
}
<div class="container">
<div class="flexbox">
<div class="inbox"> 1 </div>
<div class="inbox"> 2 </div>
<div class="inbox"> 3 </div>
<div class="inbox"> 4 </div>
<div class="inbox"> 5 </div>
</div>
<div class="box"></div>
</div>
编辑我只对左下角感兴趣,所以任何技巧都可以。
不是最好的解决方案,它可以为您提供升级的基础(例如在等式中添加 ajax)。希望对您有所帮助!
var element = document.getElementsByClassName('box')[0];
var rect = element.getBoundingClientRect();
x = rect.left;
w = rect.width;
var xa = document.getElementsByClassName('inbox');
var k = Array.from(xa).slice(-1).pop();
var rect2 = k.getBoundingClientRect();
x2 = rect2.left;
if(x+w > x2){
console.log(x + w , x2)
k.style.left =+ (x) +"px";
}
.container {
width: 120px;
height: 180px;
margin: 40px 0 0 100px;
}
.flexbox {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
width: 120px;
height: 180px;
background: pink;
}
.inbox {
position:relative;
width: 50px;
height: 50px;
margin: 5px;
background: purple;
color: #fff;
}
.box {
float: left;
width: 110px;
height: 60px;
margin: -60px 0 0 -60px;
background: orange;
}
<div class="container">
<div class="flexbox">
<div class="inbox"> 1 </div>
<div class="inbox"> 2 </div>
<div class="inbox"> 3 </div>
<div class="inbox"> 4 </div>
<div class="inbox"> 5 </div>
</div>
<div class="box"></div>
</div>
如果将 flex-wrap: wrap-reverse
与 column-reverse
flex direction 和 justify-content: flex-end
一起使用,您可以获得配置,但不能获得 顺序的元素。
请注意,元素的顺序与标记相反 - 请参阅下面的演示:
.container {
width: 120px;
height: 180px;
margin: 40px 0 0 100px;
}
.flexbox {
display: flex;
flex-direction: column-reverse; /* CHANGED */
flex-wrap: wrap-reverse; /* CHANGED */
justify-content: flex-end; /* CHANGED */
width: 120px;
height: 180px;
background: pink;
}
.inbox {
width: 50px;
height: 50px;
margin: 5px;
background: purple;
color: #fff;
margin-left: auto;
}
.box {
float: left;
width: 110px;
height: 60px;
margin: -60px 0 0 -60px;
background: orange;
}
<div class="container">
<div class="flexbox">
<div class="inbox"> 1 </div>
<div class="inbox"> 2 </div>
<div class="inbox"> 3 </div>
<div class="inbox"> 4 </div>
<div class="inbox"> 5 </div>
</div>
<div class="box"></div>
</div>
我有一个 flexbox,其元素显示如下:
问题是:有没有办法从 flexbox 外部中断流程 ,以便被阻塞的元素移动到下一个位置,如:
flexbox 是某种排序框,所以我无法在其中放置任何其他元素然后进行排序。通常列是等高的,但有时我需要第一列短一列。
我试过以各种组合方式制作方框 flow: left
和 display: inline-block
,但我无法完成。
.container {
width: 120px;
height: 180px;
margin: 40px 0 0 100px;
}
.flexbox {
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
width: 120px;
height: 180px;
background: pink;
}
.inbox {
width: 50px;
height: 50px;
margin: 5px;
background: purple;
color: #fff;
margin-left: auto;
}
.box {
float: left;
width: 110px;
height: 60px;
margin: -60px 0 0 -60px;
background: orange;
}
<div class="container">
<div class="flexbox">
<div class="inbox"> 1 </div>
<div class="inbox"> 2 </div>
<div class="inbox"> 3 </div>
<div class="inbox"> 4 </div>
<div class="inbox"> 5 </div>
</div>
<div class="box"></div>
</div>
编辑我只对左下角感兴趣,所以任何技巧都可以。
不是最好的解决方案,它可以为您提供升级的基础(例如在等式中添加 ajax)。希望对您有所帮助!
var element = document.getElementsByClassName('box')[0];
var rect = element.getBoundingClientRect();
x = rect.left;
w = rect.width;
var xa = document.getElementsByClassName('inbox');
var k = Array.from(xa).slice(-1).pop();
var rect2 = k.getBoundingClientRect();
x2 = rect2.left;
if(x+w > x2){
console.log(x + w , x2)
k.style.left =+ (x) +"px";
}
.container {
width: 120px;
height: 180px;
margin: 40px 0 0 100px;
}
.flexbox {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
width: 120px;
height: 180px;
background: pink;
}
.inbox {
position:relative;
width: 50px;
height: 50px;
margin: 5px;
background: purple;
color: #fff;
}
.box {
float: left;
width: 110px;
height: 60px;
margin: -60px 0 0 -60px;
background: orange;
}
<div class="container">
<div class="flexbox">
<div class="inbox"> 1 </div>
<div class="inbox"> 2 </div>
<div class="inbox"> 3 </div>
<div class="inbox"> 4 </div>
<div class="inbox"> 5 </div>
</div>
<div class="box"></div>
</div>
如果将 flex-wrap: wrap-reverse
与 column-reverse
flex direction 和 justify-content: flex-end
一起使用,您可以获得配置,但不能获得 顺序的元素。
请注意,元素的顺序与标记相反 - 请参阅下面的演示:
.container {
width: 120px;
height: 180px;
margin: 40px 0 0 100px;
}
.flexbox {
display: flex;
flex-direction: column-reverse; /* CHANGED */
flex-wrap: wrap-reverse; /* CHANGED */
justify-content: flex-end; /* CHANGED */
width: 120px;
height: 180px;
background: pink;
}
.inbox {
width: 50px;
height: 50px;
margin: 5px;
background: purple;
color: #fff;
margin-left: auto;
}
.box {
float: left;
width: 110px;
height: 60px;
margin: -60px 0 0 -60px;
background: orange;
}
<div class="container">
<div class="flexbox">
<div class="inbox"> 1 </div>
<div class="inbox"> 2 </div>
<div class="inbox"> 3 </div>
<div class="inbox"> 4 </div>
<div class="inbox"> 5 </div>
</div>
<div class="box"></div>
</div>