Skip to content

Instantly share code, notes, and snippets.

@Kevinlearynet
Created October 12, 2012 17:27
Show Gist options
  • Save Kevinlearynet/3880377 to your computer and use it in GitHub Desktop.
Save Kevinlearynet/3880377 to your computer and use it in GitHub Desktop.
Automatically match the height of your primary and secondary content columns by hiding asides or callouts if they over extend the main content region.
(function($){
$(window).load( function() {
// Equalize columns
$('.wrapper').equalizeColumns({
'primary_identifier' : '.primary-content',
'secondary_identifier' : '.secondary-content',
'callout_identifier' : 'aside',
'margin_offset' : 40
});
});
})(jQuery);
// Equal column heights
$.fn.equalizeColumns = function( options ) {
var settings = {
'primary_identifier' : '#content-main',
'secondary_identifier' : '#content-sub',
'callout_identifier' : '.callout',
'margin_offset' : 0
};
return this.each(function() {
// Merge settings and options
if ( options ) {
$.extend( settings, options );
}
// Get dimensions
var primary = $(settings.primary_identifier, this);
var secondary = $(settings.secondary_identifier, this);
var callout = secondary.find(settings.callout_identifier);
var primary_height = primary.outerHeight();
var secondary_height = secondary.outerHeight();
var secondary_offset = secondary.offset();
// Are there naughty callouts?
if ( primary.length > 0 && secondary.length > 0 && callout.length > 0 )
{
// Hide callouts that over extend their welcome
if ( secondary_height > primary_height )
{
callout_height = 0;
callout.each( function( index )
{
callout_height += $(this).innerHeight();
var position = $(this).position();
var offset = $(this).offset();
var offset_bottom = ( offset.top - secondary_offset.top ) + $(this).innerHeight();
// Hide callouts if the bottom of the callout is higher than the primary content
// and it's not the first callout in the sidebar
if ( offset_bottom > primary_height ) {
$(this).hide();
}
});
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment