Snippets

Rik Kendell Equal Heights

Created by Rik Kendell last modified
// jQuery wrapper
(function ($) {    
    
    // Run again when all page (and all images) fully loaded
    $(window).load(function() {
	  	equalheight('.grid .item');
	});
    
    
    // Run on window resize
    $(window).resize(function(){
	  	equalheight('.grid .item');
	});


    // Equal Heights
	equalheight = function(container){
		var currentTallest = 0,
	    	currentRowStart = 0,
	    	rowDivs = new Array(),
	    	$el,
	    	topPosition = 0;
	 	$(container).each(function() {
		   	$el = $(this);
		   	$($el).height('auto')
		   	topPosition = $el.position().top;

		   	if (currentRowStart != topPosition) {
		     	for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
		       		rowDivs[currentDiv].height(currentTallest);
		     	}
		     	rowDivs.length = 0; // empty the array
		     	currentRowStart = topPosition;
		     	currentTallest = $el.height();
		     	rowDivs.push($el);
		   	} else {
		     	rowDivs.push($el);
		     	currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
		  	}
	   		for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
		     	rowDivs[currentDiv].height(currentTallest);
		   	}
	 	});
	}
   
}); // End jQuery Wrapper

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.