forked from daneden/Baseline.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.baseline.js
More file actions
55 lines (44 loc) · 1.81 KB
/
Copy pathjquery.baseline.js
File metadata and controls
55 lines (44 loc) · 1.81 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
/*global jQuery */
/*!
* Baseline.js 1.0
*
* Copyright 2012, Daniel Eden http://daneden.me
* Released under the WTFPL license
* http://sam.zoy.org/wtfpl/
*
* Date: Wed June 20 11:39:00 2012 GMT
*/
(function( $ ) {
$.fn.baseline = function(breakpoints) {
// Set up our variables, like a good little developer
var tall, newHeight, base, paddingBorder, old = 0;
return this.each(function(){
var $this = $(this); // Set the images as objects
var setbase = function(breakpoints) { // The fun starts here
// Check if a single value or multiple breakpoints are given
if (typeof breakpoints === 'number') {
base = breakpoints;
} else if (typeof breakpoints === 'object') {
// Loop through the breakpoints and check which baseline to apply
for (key in breakpoints) {
var current = parseInt(key);
if (document.width > current && current >= old) {
base = breakpoints[key];
old = current;
}
}
}
$this.css('maxHeight', 'none'); // Remove old max-height so that we can resize up as well as down
tall = $this.height(); // Grab the height
paddingBorder = $this.outerHeight() - tall; // Get the border height
newHeight = (Math.floor(tall / base) * base) - paddingBorder; // Make up a new height based on the baseline
newHeight = (newHeight) ? newHeight : tall; // Fix for occasional 0 newHeights
$this.css('maxHeight', newHeight); // Set it!
}
setbase(breakpoints); // Call on load
$(window).resize(function(){ // And call again on resize.
setbase(breakpoints);
});
});
}
}) ( jQuery );