-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.multilists.plugin.js
65 lines (51 loc) · 1.54 KB
/
jquery.multilists.plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*************************************************
** jQuery Multi Column Lists version 1.0.5
** copyright Fred Kelly, licensed GPL & MIT
** http://fredkelly.net/
**************************************************/
(function($){
$.fn.multilists = function(options, callback) {
var defaults = {
cols: 2
};
var options = $.extend(defaults, options);
return this.each(function() {
obj = $(this);
$items = obj.children('li');
// don't waste time on empty lists
if ($items.size() > 1) {
// if no width set, container divided by columns
if (!options.colWidth) {
options.colWidth = Math.floor(obj.width()/options.cols);
}
// create our variables
var currentCol = 0;
var vertReturn = 0;
var maxHeight = 0;
// loop list items
$items.each(function(i) {
// negative top margin
if (i % Math.round($items.size()/options.cols) == 0 && i > 0) {
$(this).css('margin-top', -vertReturn);
if (vertReturn > maxHeight) {
maxHeight = vertReturn;
}
vertReturn = 0;
currentCol++;
}
// add left margin
if (currentCol > 0) {
$(this).css('margin-left', currentCol * (options.colWidth + ($(this).outerWidth(true) - $(this).width())) + 'px');
}
vertReturn += $(this).outerHeight();
});
// set height on containing list
obj.height(vertReturn > maxHeight ? vertReturn : maxHeight);
}
// if the callback is a function, call it...
if (typeof callback == 'function') {
callback.call(this);
}
});
};
})(jQuery);