Skip to content

Instantly share code, notes, and snippets.

@firedev
Created March 23, 2015 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firedev/e33c9b923110b2a06a83 to your computer and use it in GitHub Desktop.
Save firedev/e33c9b923110b2a06a83 to your computer and use it in GitHub Desktop.
Height Equalizer – new Equializer('.elements') will keep all elements the same height
class @Equalizer
constructor: (selector) ->
@init $(selector)
init: ($collection)->
@attachTo($collection) if $collection.length
attachTo: ($collection) ->
@collection = $collection
@setTimeoutFunction(@equalizeHeight, @collection)
setTimeoutFunction: (fn, param) ->
$(window).resize ->
clearTimeout a
a = setTimeout((->
fn(param)
), 50)
fn(param)
equalizeHeight: (collection)->
max = -Infinity
collection.height ''
collection.each ->
h = $(@).height()
max = h if h > max
collection.height max + 'px'
(function() {
this.Equalizer = (function() {
function Equalizer(selector) {
this.init($(selector));
}
Equalizer.prototype.init = function($collection) {
if ($collection.length) {
return this.attachTo($collection);
}
};
Equalizer.prototype.attachTo = function($collection) {
this.collection = $collection;
return this.setTimeoutFunction(this.equalizeHeight, this.collection);
};
Equalizer.prototype.setTimeoutFunction = function(fn, param) {
$(window).resize(function() {
var a;
clearTimeout(a);
return a = setTimeout((function() {
return fn(param);
}), 50);
});
return fn(param);
};
Equalizer.prototype.equalizeHeight = function(collection) {
var max;
max = -Infinity;
collection.height('');
collection.each(function() {
var h;
h = $(this).height();
if (h > max) {
return max = h;
}
});
return collection.height(max + 'px');
};
return Equalizer;
})();
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment