I have multiple images which I want to resize to a fixed height and the width will be resized in the ratio of the resized height and i want to achieve this using jQuery. So I gave all the images the same class attribute and I ran this code:
<img src="img1.jpg" class="imgs">
<img src="img2.jpg" class="imgs">
<img src="img3.jpg" class="imgs">
<script>
$(document).ready(function(){
$('.imgs').each(function(){
oldH = $(this).naturalHeight;
oldW = $(this).naturalWidth;
divH = "500px";
if(oldH > divH){
newH = divH;
calW = newH / oldH;
newW = calW * oldW;
$(this).naturalHeight = newH;
$(this).naturalWidth = newW;
} else{
newH = oldH;
newW = oldW;
$(this).naturalHeight = newH;
$(this).naturalWidth = newW;
}
})
});
</script>
But the images are not resizing, I am still new to jQuery so I am not to good. I don't know if that's the right approach to achieve this. Please if it's not I need a clue on how to achieve this.