Created
February 25, 2019 09:07
-
-
Save ivanmarkovich/c64adf07a047ce976338fcade8d89a6f to your computer and use it in GitHub Desktop.
img cover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// img cover | |
var imageCoverFunc = function() { | |
//set size | |
var th = $(this).height(),//box height | |
tw = $(this).width(),//box width | |
im = $(this).children('img'),//image | |
ih = im.height(),//inital image height | |
iw = im.width();//initial image width | |
if ((th/tw) > (ih/iw)){ | |
im.addClass('wh').removeClass('ww');//set height 100% | |
} else { | |
im.addClass('ww').removeClass('wh');//set width 100% | |
} | |
//set offset | |
var nh = im.height(),//new image height | |
nw = im.width(),//new image width | |
hd = (nh-th)/2,//half dif img/box height | |
wd = (nw-tw)/2;//half dif img/box width | |
if (hd < 1) {hd = 0;} | |
if (wd < 1) {wd = 0;} | |
im.css({marginLeft: '-'+wd+'px', marginTop: '-'+hd+'px'});//offset left | |
} | |
// $('.img-cover img')[0].addEventListener('load', function() { | |
// $(this).closest('.img-cover').each(imageCoverFunc); | |
// }, false); | |
$('.img-cover img').each(function(){ | |
if ( !this ) return; | |
this.addEventListener('load', function() { | |
$(this).closest('.img-cover').each(imageCoverFunc); | |
}, false); | |
}) | |
$('.img-cover').each(imageCoverFunc); | |
window.onresize = function(){ | |
$('.img-cover').each(imageCoverFunc); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment