-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.equalheights.js
107 lines (86 loc) · 2.27 KB
/
jquery.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* jQuery Equal Heights by row
*
* Copyright (c) 2016 iBird Rose & KFan
* Dual licensed under the MIT and GPL licenses.
*
* Version 1.0.0
*/
;jQuery.fn.extend({
equalHeights: function(options) {
var self = this;
options = $.extend({
innerItem: false,
parent: $(this).parent(),
byRow: true
}, options);
this.setItemsWithInner = (function (items, height, inner, parent) {
if (inner) {
if (height == "auto") {
$(items).find(inner).height(height);
} else {
$(items).find(inner).each(function () {
var nHeight = height;
$(this).siblings().each(function () {
nHeight -= $(this).outerHeight();
});
var p = this;
do {
var p = $(p).parent();
nHeight -= parseInt($(p).css("padding-top"));
nHeight -= parseInt($(p).css("padding-bottom"));
} while (!$(p).is(parent));
$(this).height(nHeight);
});
}
} else {
$(items).height(height);
}
});
var allEachItems = $(this);
$(options.parent).each(function () {
var eachItems = [];
var items = [];
var itemsWidth = 0;
var maxHeight = 0;
var selfParent = this;
if (options.parent.length > 1) {
eachItems = $(this).find(allEachItems);
} else {
eachItems = allEachItems;
}
self.setItemsWithInner(eachItems, "auto", options.innerItem);
$(eachItems).each(function () {
var itemWidth = $(this).outerWidth(true);
var itemHeight = $(this).height();
if (itemWidth + itemsWidth > $(selfParent).width() && items.length > 0 && options.byRow) {
self.setItemsWithInner(items, maxHeight, options.innerItem, selfParent);
items = [this];
maxHeight = itemHeight;
itemsWidth = itemWidth;
return;
}
if (maxHeight < itemHeight) {
maxHeight = itemHeight;
}
items.push(this);
itemsWidth += itemWidth;
});
if (items.length > 0) {
self.setItemsWithInner(items, maxHeight, options.innerItem, selfParent);
items = [];
}
});
return this;
}
});
$(document).ready(function () {
$("[data-equalheights]").each(function () {
var key = $(this).attr("data-equalheights");
var items = $(this).find(key);
$(items).equalHeights();
$(window).resize(function () {
$(items).equalHeights();
});
});
});