I want to be able to use jQuery to get the height / width of multiple images before the images are fully loaded into the page. I then need to set the height and margin of a image based on the the image width and height which is being loaded.
$(".review-content .photos .img").each(function() {
if (!$(this).hasClass("showimg")) {
var tarObj = $(this),
imgsrc = $(this).find("img"),
blockWid = tarObj.width(),
blockHei = tarObj.height(),
imgWid = imgsrc.width(),
imgHei = imgsrc.height(),
tarImg = tarObj.find("img"),
formatHei = blockWid * imgHei / imgWid;
if (formatHei > blockHei) {
tarImg.css("width", blockWid + "px");
var marHei = -(formatHei - blockHei) / 2;
tarImg.css("margin-top", marHei + "px")
} else {
tarImg.css("height", blockHei + "px");
var formatWid = blockHei * imgWid / imgHei,
marWid = -(formatWid - blockWid) / 2;
tarImg.css("margin-left", marWid + "px")
}
tarObj.addClass("showimg")
tarImg.attr("data-width", imgWid)
tarImg.attr("data-height", imgHei)
}
})
Now the issue in above code is even if i add
$(".review-content .photos .img img").load(function() {
Before each function it will apply the calculations on some of the images and rest of the images will be messed up so someone please guide me in the right direction on how i can get the width and height of each image and apply the css accordingly to every image in the page