forked from status201/jquery-equalheights
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.equalheights.js
More file actions
75 lines (63 loc) · 1.84 KB
/
Copy pathjquery.equalheights.js
File metadata and controls
75 lines (63 loc) · 1.84 KB
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
/**
* Equal heights little jQuery plugin by Status201
* @version 1.0.7
* @requires jQuery 1.8+
* @author Gijs Oliemans <gijs[at]status201.nl>
* @license MIT
*/
(function( $ ) {
$.fn.equalHeights = function( options ) {
var defaults = {
onResize: true,
onLoad: true
};
var settings = $.extend( {}, defaults, options );
var topPositions = {},
foundPosition = 0,
$el = [],
curHeight = 0,
topPosition = 0,
resizeTimer,
$elements = $(this);
if ($elements.length < 2) return this;
if ( settings.onResize ) {
$( window ).resize(function() {
if ( resizeTimer ) window.clearTimeout(resizeTimer);
resizeTimer = window.setTimeout(function() {
$elements.equalHeights( { onResize: false, onLoad: false } );
}, 100);
});
};
if ( settings.onLoad ) {
$( window ).on('load',(function() {
$elements.equalHeights( { onResize: false, onLoad: false } );
}));
}
$elements.each(function() {
$el = $(this);
$el.height('auto');// restore original height from possible previous equalHeights()
curHeight = $el.height();
if ( curHeight > 0 ) {
topPosition = $el.position().top;
foundPosition = topPosition in topPositions;
if(!foundPosition) {
// First at this position, only store and set height
topPositions[topPosition] = curHeight;
$el.height(topPositions[topPosition]);
} else {
if(curHeight > topPositions[topPosition]) {
// Tallest so far for this position, remember tallest and stretch others on this position
topPositions[topPosition] = curHeight;
$($elements).filter(function() {
return $(this).position().top == topPosition;
}).height(curHeight);
} else {
// Same or less high, maximize this one
$el.height(topPositions[topPosition]);
}
}
}
});
return this;
};
}( jQuery ));