试试这个小提琴
Step 1:计算thumb box的实际宽度即width + 2*(border + margin + padding)
第 2 步:连续计算拇指的数量(这是您必须考虑的地方)我所做的是我检查了每个拇指的顶部 请参阅 Function
calculate_in1Row ()
HTML:
<div class="red">
<div class="green">Logo</div>
<div class="blue">
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
</div>
</div>
CSS:
.red {border:1px solid red; }
.green{border:1px solid green; margin:2px;}
.blue{border:1px solid blue; margin:2px; overflow:auto;}
.thumb {border:1px solid black; width:40px; height:40px; margin:2px; padding:3px; float:left}
jQuery:
$(document).ready(function() {
var tw = $(".thumb").width() + 2 * (1 + 2 + 3); // width + 2*(border+margin + padding)
tw = tw * calculate_in1Row()
$(".blue").width(tw);
$(".green").width(tw);
});
// function ti calculate no of thumb box in single row
function calculate_in1Row() {
var in1Row = 0;
$('.blue .thumb').each(function() {
if ($(this).prev().length > 0) {
if ($(this).position().top != $(this).prev().position().top) return false;
in1Row++;
}
else {
in1Row++;
}
});
return in1Row;
}