-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequalHeight.js
83 lines (58 loc) · 1.79 KB
/
equalHeight.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
/*
equalHeight.js
Make heights match in a set of matching elements
Author: Felipe Arosemena
Date: 12 Nov 2014
Usage: $('.el').equalHeight();
*/
(function($) {
$.fn.extend({
equalHeight: function(options) {
var els = this,
opt = $.extend({
stopAt: false
},options);
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) { func.apply(context, args); }
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) { func.apply(context, args); }
};
}
var setHeight = function(){
var groups = {};
els.each(function(){
var $this = $(this),
data = $this.attr('data-equal-height'),
height = $this.css('height','auto').outerHeight();
if(groups.hasOwnProperty(data)){
groups[data].count += 1;
if(height > groups[data].maxH) {
groups[data].maxH = height;
}
} else {
groups[data] = {};
groups[data].count = 1;
groups[data].maxH = height;
}
});
els.each(function(){
var $this = $(this);
$.each(groups,function(k,v){
$this.filter('[data-equal-height="'+k+'"]').css('height',groups[k].maxH);
});
});
};
var debouncedSetHeight = debounce(setHeight,0);
setHeight();
$(window).on('resize',debouncedSetHeight);
}
});
})(jQuery);