这是一个应该做你想做的脚本:
function adaptTitleWidth() {
// reset default style
$(".box").css("background","purple");
// get the first row of boxes (the first box and the boxes having the same offset().top
firstRowBoxes = $(".box").filter(function() {
return $(this).offset().top === $(".box:first").offset().top;
// changes the first row of boxes background to blue
}).css("background","blue");
// changing .logo.width() : number of boxes on the first row * outer width of the boxes, margin included - the last margin-right
$(".logo").width(firstRowBoxes.length * $(".box:first").outerWidth(true) - parseInt($(".box:first").css("margin-right")));
}
$(function() {
adaptTitleWidth();
window.onresize = function(event) {
adaptTitleWidth();
}
});
jsBin 示例在这里