gpt4 book ai didi

javascript - 双边框 div 应该合并 css

转载 作者:技术小花猫 更新时间:2023-10-29 12:09:22 25 4
gpt4 key购买 nike

如果你看这个fiddle你会看到边界不会合并。
CSS:

div{
float:left;
background-color:moccasin;
width:100px;
height:100px;
border:1px solid tomato;
}

div的数量是随机的,我只能给它1个class/id。
另请记住,页面大小可以调整,一行中的 div 数量也可以改变。

我已经尝试过 margin-left:1px; 和 last-child/nth-child() 选择器,但是当你调整屏幕大小时它们不起作用或者它仍然会产生不需要的边框。

编辑: 当我给 div margin-left:-1px; 并给第一个 div 时,我无法移动单个像素的 div margin-left:1px; 我会在接下来的行中得到不需要的结果。

最佳答案

直接添加到div

margin-top: -1px;
margin-left: -1px;

检查以下示例:

div{
float:left;
background-color:moccasin;
width:100px;
height:100px;
border:1px solid tomato;
margin-left: -1px;
margin-top: -1px;
}
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

JS 的另一种解决方案这是 DEMO

CSS:

div{
float:left;
background-color:moccasin;
width:100px;
height:100px;
border-bottom:1px solid tomato;
border-right:1px solid tomato;
}
div:first-child{
border-left:1px solid tomato;
}
div.first-row {
border-top: 1px solid tomato;
}

jQuery:

var borderCollapse = function() {
var div = $('div');
var divWidth = 0;
var rows = 1;
div.each(function() {
var that = $(this);
var div = $('div');
var width = div.parent().width();
var thisWidth = $(this).width();

if (divWidth < width) {
that.prev().not('div:first-child').css({'border-left':'0'});
divWidth += parseInt(thisWidth);
} else {
that.prev().css({'border-left':'1px solid tomato'});
divWidth = 2 * thisWidth;
rows += 1;
}

if (rows <= 1) {
that.prev().addClass('first-row');
} else {
that.prev().removeClass('first-row');
}

});
}

borderCollapse();

$(window).resize(function() {
borderCollapse();
});

关于javascript - 双边框 div 应该合并 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27228686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com