Advertisement
jongsi

[DO NOT DELETE] Grid Code

Jan 18th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. (function($){
  2.  
  3. //Finding min and max values in array from http://snippets.dzone.com/posts/show/769
  4. Array.prototype.min = function(){ return Math.min.apply({},this) };
  5. Array.prototype.max = function(){ return Math.max.apply({},this) };
  6.  
  7. $.fn.masonry = function() {
  8. this.each(function() {
  9.  
  10. var wall = $(this);
  11.  
  12. if ( wall.children().length > 0 ) { // check if the element has anything in it
  13.  
  14. if( wall.children('.masonryWrap').length == 0 ) { // checks if the masonryWrap div is already there
  15. wall.wrapInner('<div class=\"masonryWrap\"></div>');
  16. }
  17. var mWrap = wall.children('.masonryWrap');
  18.  
  19. var brick = mWrap.children();
  20. var brickW = brick.outerWidth(true);
  21. var colCount = Math.floor( mWrap.width() / brickW ) ;
  22.  
  23. var colH=new Array();
  24. for ( i=0; i < colCount; i++) {
  25. colH[ i ] = 0 ;
  26. }
  27.  
  28. mWrap.css({ position: 'relative' });
  29.  
  30. brick.css({
  31. float: 'none',
  32. position: 'absolute',
  33. display: 'block'
  34. })
  35. .each(function(){
  36. for ( i=colCount-1; i > -1; i-- ) {
  37. if ( colH[ i ] == colH.min() ) {
  38. var thisCol = i;
  39. }
  40. }
  41. $(this).css({
  42. top: colH[ thisCol ],
  43. left: brickW * thisCol
  44. });
  45. colH[ thisCol ] += $(this).outerHeight(true);
  46. });
  47.  
  48. mWrap.height( colH.max() );
  49. }
  50.  
  51. return this;
  52. });
  53. };
  54. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement