This repository has been archived by the owner on Jan 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
equalHeights.js
89 lines (68 loc) · 1.67 KB
/
equalHeights.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// ///////////////////////////////////////////////
// name: equalHeights
// purpose: to maintain equal heights across columns
// usage: markup required is:
// ////// <div class="equal-height-group">
// ////// <div class="equal-height-item"></div>
// ////// <div class="equal-height-item"></div>
// ////// </div>
//
// options: el: a jQuery element object
// opts: {cutoff: integer}
// ///////////////////////////////////////////////
app.EqualHeights = (function($, _, app) {
var def = function(el, opts) {
var self = this;
this.$els = {
'item' : el,
'children' : '.equal-height-item'
};
this.opts = opts;
this.heights = [];
this.waiter = _.debounce(function(e) {
self.destroy();
self.measure();
}, 200);
init.call(this);
};
var init = function() {
this.bind();
this.measure();
};
def.prototype = {
bind: function() {
var self = this;
$(window).resize(function() {
if(self.opts.cutoff && $(window).width() <= self.opts.cutoff) {
self.destroy();
} else {
self.waiter();
}
});
},
measure: function() {
var self = this;
this.heights = [];
$(this.$els.children, this.$els.item).each(function(){
self.heights.push($(this).outerHeight(true));
});
this.set();
},
findLargest: function() {
return Math.max.apply(Math, this.heights);
},
set: function() {
var base = this.findLargest();
this.applyStyles(base);
},
destroy: function() {
this.applyStyles('');
},
applyStyles: function(h) {
$(this.$els.children, this.$els.item).each(function(){
$(this).css('height', h);
});
}
}
return def;
}).call(this, jQuery, _, app);