Skip to content

Instantly share code, notes, and snippets.

@aerith
Created August 31, 2012 08:58
Show Gist options
  • Save aerith/3550505 to your computer and use it in GitHub Desktop.
Save aerith/3550505 to your computer and use it in GitHub Desktop.
(function (w, d) {
// See: https://gist.github.com/1250370
// See: http://d.hatena.ne.jp/amachang/20070611/1181554170
var slice = Array.prototype.slice;
var BoxHeight = function (tagName, object) {
this.top = 0;
this.height = 0;
this.target = [];
this.call(tagName, object);
};
BoxHeight.prototype = {
call: function (tagName, object) {
var self = this;
var parent = this.$(object) || d;
var elements = parent.getElementsByTagName(tagName);
this.applyEach(slice.call(elements), function (e, i) {
var style = self.getStyle(e);
if (e.offsetTop !== self.top) {
if(self.target.length > 1) {
self.applyEach(self.target, self.setHeight, self.height);
}
self.target = [];
self.height = 0;
self.top = e.offsetTop;
}
self.target.push(e);
self.height = Math.max(
parseInt(self.getStyle(e).height, 10),
parseInt(self.height, 10)
);
});
},
setHeight: function (e, i, height) {
// binded context
e.style.height = height.toString() + 'px';
},
getStyle: function (element) {
return element.currentStyle || d.defaultView.getComputedStyle(element, '');
},
$: function (object) {
if (object.nodeType && object.nodeType === 1) {
return object;
}
return document.getElementById(object);
},
applyEach: function (collection, iterator) {
var args = slice.call(arguments, 2);
var result = [];
for (var i = 0, l = collection.length; i < l; i++) {
result.push(iterator.apply(null, [collection[i], i].concat(args)));
}
return result;
}
}
w.BoxHeight = BoxHeight;
})(window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment