forked from daneden/Baseline.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline.js
63 lines (54 loc) · 1.39 KB
/
baseline.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
/*
* BaselineJS.js 1.0
*
* Copyright 2012, Ben Howdle http://twostepmedia.co.uk
* Released under the WTFPL license
* http://sam.zoy.org/wtfpl/
*
* Date: Thu June 21 20:58 2012 GMT
*/
/*
//call like so
baseline.init('img', '24');
baseline.init('.post img', '48');
baseline.init('#content img', {480: 30, 640: 35}); // an object of breakpoints, ie. 480px or above, baseline is 30px.
*/
var baseline = function(){
var tall, newHeight, target, imgl, cur, images = [];
return {
init: function(selector, target){
if(selector === undefined || target === undefined) return false;
this.images = document.querySelectorAll(selector);
this.setbase(this.images);
this.tellmetarget(target);
var me = this;
window.onresize = function() {
me.tellmetarget(target);
me.setbase(me.images);
};
},
tellmetarget: function(target){
if(typeof target === 'number'){
this.target = target;
} else if(typeof target === 'object'){
for(x in target){
if(document.width > x){
this.target = target[x];
}
}
}
},
setbase: function(imgs){
this.imgl = imgs.length;
if(this.imgl){
while(this.imgl--){
this.cur = imgs[this.imgl];
this.cur.style.maxHeight = 'none';
this.tall = this.cur.offsetHeight;
this.newHeight = Math.floor(this.tall / this.target) * this.target;
this.cur.style.maxHeight = this.newHeight + 'px';
}
}
}
};
}();