-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageMiddle.txt
30 lines (28 loc) · 1.1 KB
/
imageMiddle.txt
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
imgMiddle: function(cssMethod){
if( ! $(this).length ) return;
$(this).each(function(){
var img = $(this).find('img'),
areaH = $(this).height(),
imgH = img.innerHeight();
if( imgH > 0 ){
setImageCss(imgH, areaH, img);
} else {
img.load(function(){
imgH = $(this).innerHeight();
setImageCss(imgH, areaH, $(this));
});
}
});
function setImageCss(imgH, areaH, img){
if( imgH > 0 && imgH < areaH ){
var vTop = (areaH - imgH) / 2;
if(cssMethod == 'top') {
img.css({position:'relative', top: vTop});
} else if (cssMethod == 'padding-top') {
img.css({ paddingTop: vTop});
} else {
img.css({marginTop: vTop});
}
}
}
},