Skip to content

Instantly share code, notes, and snippets.

@anointed
Created July 14, 2012 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anointed/3113675 to your computer and use it in GitHub Desktop.
Save anointed/3113675 to your computer and use it in GitHub Desktop.
Equal Height columns only if browser window is wider than 481px
// global variables
var compareWidthColumn, //previous width used for comparison
detectorColumn, //the element used to compare changes
smallScreenColumn, // Size of maximum width of single column media query
jQuery(document).ready(function($) {
//set the initial values
detectorColumn = jQuery('.js');
compareWidthColumn = detectorColumn.width();
smallScreenColumn = '481';
if ($(window).width() > smallScreenColumn) {
$('.equal').matchHeights({
minHeight: null, // optional minimum height setting
maxHeight: null, // optional maximum height setting, forced height instead of min-height
extension: 0, // optional amount to add to calculated height
overflow: false // optional setting for overflow. Default is false; overflow attribute not set
});
}
jQuery(window).resize(function() {
//compare everytime the window resize event fires
if (detectorColumn.width() != compareWidthColumn) {
//a change has occurred so update the comparison variable
compareWidthColumn = detector.width();
if (compareWidthColumn > smallScreenColumn) {
$('.equal').matchHeights({
minHeight: null, // optional minimum height setting
maxHeight: null, // optional maximum height setting, forced height instead of min-height
extension: 0, // optional amount to add to calculated height
overflow: false // optional setting for overflow. Default is false; overflow attribute not set
});
}
}
});
});
@anointed
Copy link
Author

The Problem:
This script works correctly on page load but has a number of problems if the user resizes the browser.

  1. The height of the columns does not recalculate - thus leading to the possibility of both columns becoming taller than needed if the browser is shrunk.
    *function needs to rerun and calculate the height on resize
  2. If the browser starts out > than 481px, then the columns are set evenly, however if the user resizes the browser smaller than 481 px, the equalHeight function does not remove itself.

Reason:
I don't want to run the function if the browser is less than 481px because I am using a CSS media query to 'stack' the divs on mobile view.

@anointed
Copy link
Author

Just found your script last night and had a question about it if you have a few mins.
mikeadesign/matchHeights#1

Ok, I managed to get a demo of the issue uploaded
http://shawngaffney.com/scss/

Easy way to reproduce the problem:

  1. resize your browser window to about 1/2 screen somewhere around 700px, you will see the blue sidebar get longer like it should.
  2. resize your browser wider and you will see that the blue sidebar stays much taller than it needs to be.
  3. resize your browser way down to less than 480px and you will notice that the content area and the sidebar are huge as the script is not being removed.
    *I only wan this script to activate on screen size wider than 480px as you can see in the jquery on git.

Can you help with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment