不知道是否有插件,但你去(伴奏小提琴:http: //jsfiddle.net/58akv/1/):
jQuery:
// This is me being lazy and not wanting to copy/paste box 50 times
var box = $('div.box');
for (var i = 1; i < 50; i++) {
box.clone().text(i).appendTo(box.parent());
}
// Now that we have all elements
var i = 0;
// Specify how many you'd like per row, if you want to
var rowsPerColumn = 0;
var cols = 0;
$('.box').each(function() {
// remove float
$(this).css("float", "none");
if ($(this).parent('.col').length == 0) {
// if rowsPerColumn is 0 or undefined, figure it out ourselves
if (rowsPerColumn == 0 || !rowsPerColumn) {
// calculate how many fit into given width
var totalColumns = Math.floor($("#wrapper")[0].clientWidth / $(this).outerWidth(true));
// calculate how many boxes go into each column
rowsPerColumn = Math.ceil($('.box', '#wrapper').length / totalColumns);
}
// wrap this and next in col
var parent = $(this).parent();
var els = $(this).add($(this).siblings('.box').slice(0, rowsPerColumn-1));
parent.append($('<div></div>').addClass('col').append(els).attr('id', 'col'+ (++cols).toString()))
}
});
CSS(例如目的):
.box {
background-color: #333;
width: 90px;
height: 90px;
margin: 5px;
color: #eee;
font-size: 2em;
float: left;
}
.col { float: left; }
HTML(例如目的):
<div id="wrapper">
<div class="box">0</div>
</div>