2 // http://welcome.totheinter.net/columnizer-jquery-plugin/
3 // created by: Adam Wulf adam.wulf@gmail.com
6 $.fn.columnize = function(options) {
14 var options = $.extend(defaults, options);
16 return this.each(function() {
18 var maxHeight = $inBox.height();
19 var $cache = $('<div></div>'); // this is where we'll put the real content
21 var columnizing = false;
22 $cache.append($inBox.children().clone());
26 $(window).resize(function() {
27 if(!options.buildOnce && $.browser.msie){
28 if($inBox.data("timeout")){
29 clearTimeout($inBox.data("timeout"));
31 $inBox.data("timeout", setTimeout(columnizeIt, 200));
32 }else if(!options.buildOnce){
40 * return a node that has a height
41 * less than or equal to height
43 * @param putInHere, a dom element
44 * @$pullOutHere, a jQuery element
46 function columnize($putInHere, $pullOutHere, $parentColumn, height){
47 while($parentColumn.height() < height &&
48 $pullOutHere[0].childNodes.length){
49 $putInHere.append($pullOutHere[0].childNodes[0]);
51 if($putInHere[0].childNodes.length == 0) return;
53 // now we're too tall, undo the last one
54 var kids = $putInHere[0].childNodes;
55 var lastKid = kids[kids.length-1];
56 $putInHere[0].removeChild(lastKid);
57 var $item = $(lastKid);
58 if($item[0].nodeType == 3){
59 // it's a text node, split it up
60 var oText = $item[0].nodeValue;
61 var counter2 = options.width / 8;
63 while($parentColumn.height() < height && oText.length){
64 if (oText.indexOf(' ', counter2) != '-1') {
65 columnText = oText.substring(0, oText.indexOf(' ', counter2));
69 $putInHere.append(document.createTextNode(columnText));
70 if(oText.length > counter2){
71 oText = oText.substring(oText.indexOf(' ', counter2));
77 $item[0].nodeValue = oText;
83 if($pullOutHere.children().length){
84 $pullOutHere.prepend($item);
86 $pullOutHere.append($item);
90 function split($putInHere, $pullOutHere, $parentColumn, height){
91 if($pullOutHere.children().length){
92 $cloneMe = $pullOutHere.children(":first");
93 $clone = $cloneMe.clone();
94 if($clone.attr("nodeType") == 1){
95 $putInHere.append($clone);
96 if($clone.is("img") && $parentColumn.height() < height + 20){
98 }else if(!$cloneMe.hasClass("dontsplit") && $parentColumn.height() < height + 20){
100 }else if($clone.is("img") || $cloneMe.hasClass("dontsplit")){
104 columnize($clone, $cloneMe, $parentColumn, height);
105 if($cloneMe.children().length){
106 split($clone, $cloneMe, $parentColumn, height);
114 function singleColumnizeIt() {
115 if ($inBox.data("columnized") && $inBox.children().length == 1) {
118 $inBox.data("columnized", true);
119 $inBox.data("columnizing", true);
122 $inBox.append($("<div class='first last column'></div>")); //"
123 $col = $inBox.children().eq($inBox.children().length-1);
124 $col.append($cache.clone());
126 $inBox.data("columnizing", false);
130 function columnizeIt() {
131 if(lastWidth == $inBox.width()) return;
132 lastWidth = $inBox.width();
134 var numCols = Math.round($inBox.width() / options.width);
135 if(options.columns) numCols = options.columns;
136 // if ($inBox.data("columnized") && numCols == $inBox.children().length) {
140 return singleColumnizeIt();
142 if($inBox.data("columnizing")) return;
143 $inBox.data("columnized", true);
144 $inBox.data("columnizing", true);
147 $inBox.append($("<div style='width:" + (Math.round(100 / numCols) - 2)+ "%; padding: 3px; float: left;'></div>")); //"
148 $col = $inBox.children(":last");
149 $col.append($cache.clone());
150 maxHeight = $col.height();
153 var targetHeight = maxHeight / numCols;
154 var firstTime = true;
156 for(var loopCount=0;loopCount<maxLoops;loopCount++){
158 var $destroyable = $cache.clone();
159 $destroyable.css("visibility", "hidden");
160 // create the columns
161 for (var i = 0; i < numCols; i++) {
163 var className = (i == 0) ? "first column" : "column";
164 var className = (i == numCols - 1) ? ("last " + className) : className;
165 $inBox.append($("<div class='" + className + "' style='width:" + (Math.round(100 / numCols) - 2)+ "%; float: left;'></div>")); //"
168 // fill all but the last column
169 for (var i = 0; i < numCols-1; i++) {
170 var $col = $inBox.children().eq(i);
171 columnize($col, $destroyable, $col, targetHeight);
172 split($col, $destroyable, $col, targetHeight);
174 // the last column in the series
175 $col = $inBox.children().eq($inBox.children().length-1);
176 while($destroyable.children().length) $col.append($destroyable.children(":first"));
177 var afterH = $col.height();
178 var diff = afterH - targetHeight;
182 $inBox.children().each(function($inBox){ return function($item){
183 var h = $inBox.children().eq($item).height();
188 var avgH = totalH / numCols;
190 targetHeight = avgH + 30;
191 }else if(Math.abs(avgH-targetHeight) > 20){
194 loopCount = maxLoops;
196 $inBox.append($("<br style='clear:both;'>"));
198 $inBox.data("columnizing", false);