Created
February 14, 2012 16:24
-
-
Save anonymous/1827897 to your computer and use it in GitHub Desktop.
Justified Version + Margin Option (for leaving space on Left and Right sides of the page)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
jQuery Wookmark plugin 0.1 | |
@name jquery.wookmark.js | |
@author Christoph Ono (chri@sto.ph or @gbks) | |
@version 0.1 | |
@date 12/14/2011 | |
@category jQuery plugin | |
@copyright (c) 2009-2011 Christoph Ono (www.wookmark.com) | |
@license Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license. | |
*/ | |
$.fn.wookmark = function(options) { | |
// Apply defaults for options if they are not provided. | |
if(!options.container) options.container = $(window); | |
if(!options.offset) options.offset = 2; | |
if(!options.itemWidth) { | |
options.itemWidth = $(this[0]).width(); | |
} | |
if(!options.margin) { | |
options.margin = 50; | |
} | |
// Calculate basic layout parameters. | |
var columnWidth = options.itemWidth + options.offset; | |
var containerWidth = options.container.width(); | |
var columns = Math.floor((containerWidth+options.offset-(options.margin*2))/columnWidth); | |
//var offset = Math.round((containerWidth - (columns*columnWidth-options.offset))/2); | |
var offset = Math.round((containerWidth - (columns*columnWidth-options.offset) - (options.margin * 2))/(columns-1)); | |
// Prepare Array to store height of columns. | |
var heights = []; | |
while(heights.length < columns) heights.push(0); | |
// Loop over items. | |
var item, top, left, i=0, k=0, length=this.length, shortest=null, shortestIndex=null, bottom = 0, margin=0; | |
for(; i<length; i++ ) { | |
item = $(this[i]); | |
// Find the shortest column. | |
shortest = null; | |
shortestIndex = 0; | |
for(k=0; k<columns; k++) { | |
if(shortest == null || heights[k] < shortest) { | |
shortest = heights[k]; | |
shortestIndex = k; | |
} | |
} | |
if(!shortestIndex) | |
margin = options.margin; | |
// Postion the item. | |
item.css({ | |
position: 'absolute', | |
top: shortest+'px', | |
left: (shortestIndex*columnWidth + (shortestIndex*offset) + margin)+'px' | |
}); | |
// Update column height. | |
heights[shortestIndex] = shortest + item.outerHeight() + options.offset; | |
bottom = Math.max(bottom, heights[shortestIndex]); | |
} | |
// Set container height to height of the grid. | |
options.container.css('height', bottom+'px'); | |
// Display items (if hidden). | |
this.show(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment