-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjquery.equalheightcolumns.js
123 lines (97 loc) · 4.53 KB
/
jquery.equalheightcolumns.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*global jQuery */
/*!
* equalHeightColumns.js 1.2
* https://github.com/PaulSpr/jQuery-Equal-Height-Columns
*
* Copyright 2014, Paul Sprangers http://paulsprangers.com
* Released under the WTFPL license
* http://www.wtfpl.net
*
* Date: Sat Dec 13 11:30:00 2014 +0100
*/
(function( $ ){
$.fn.equalHeightColumns = function( options ) {
defaults = {
minWidth: -1, // Won't resize unless window is wider than this value
maxWidth: 99999, // Won't resize unless window is narrower than this value
setHeightOn: 'min-height', // The CSS attribute on which the equal height is set. Usually height or min-height
defaultVal: 0, // Default value (for resetting columns before calculation of the maximum height) for the CSS attribute defined via setHeightOn, e.g. 'auto' for 'height' or 0 for 'minHeight'
equalizeRows: false, // Give every column in indiviual rows even height. Every row can have a different height this way
checkHeight: 'height' // Which height to check, using box-sizing: border-box, innerHeight is probably more appropriate
};
var $this = $(this); // store the object
options = $.extend( {}, defaults, options ); // merge options
// Resize height of the columns
var resizeHeight = function () {
// Get window width
var windowWidth = $(window).width();
var currentElements = Array();
// Check to see if the current browser width falls within the set minWidth and maxWidth
if( options.minWidth < windowWidth && options.maxWidth > windowWidth ){
var height = 0;
var highest = 0;
var yPos = 0;
// Reset heights
$this.css( options.setHeightOn, options.defaultVal );
// Figure out the highest element
$this.each( function(){
if( options.equalizeRows ){
// Check if y position of the element is bigger, if so, it's on another row.
// Make sure that the height is only set relative to elements in the same row.
var elYPos = $(this).position().top;
if( elYPos != yPos ){
// new row, so set the height of the elements of the previous row
if( currentElements.length > 0 ) {
$(currentElements).css(options.setHeightOn, highest);
// clear the array and reset values for the new row
highest = 0;
currentElements = [];
}
// get element elYPos again since it might have changed because of the resize
yPos = $(this).position().top;
}
currentElements.push(this);
}
// do the height check and if it's the highest, set it as such
height = $(this)[options.checkHeight]();
if( height > highest ){
highest = height;
}
} );
if( !options.equalizeRows ){
// Set that height on the elements at once
$this.css( options.setHeightOn, highest );
}
else{
// set height on elements in last row
$(currentElements).css( options.setHeightOn, highest );
}
}
else{
// Add check so this doesn't have to happen everytime
$this.css(options.setHeightOn, options.defaultVal);
}
};
// Call once to set initially
resizeHeight();
// Call on resize. Opera debounces their resize by default.
$(window).resize(resizeHeight);
// Also check if any images are present and recalculate when they load
// there might be an optimization opportunity here
$this.find('img').load( resizeHeight );
// If afterLoading is defined, add a load event to the selector
if ( typeof options.afterLoading !== 'undefined' ) {
$this.find(options.afterLoading).load( resizeHeight );
}
// If afterTimeout is defined use it a the timeout value
if ( typeof options.afterTimeout !== 'undefined' ) {
setTimeout(function(){
resizeHeight();
// check afterLoading again, to make sure that dynamically added nodes are present
if ( typeof options.afterLoading !== 'undefined' ) {
$this.find(options.afterLoading).load( resizeHeight );
}
}, options.afterTimeout);
}
};
})( jQuery );