Enlarges / reduces the size of the image that preserves the aspect ratio when pressed

I want to be able to enlarge (enlarge) and reduce (reduce) the image and maintain the aspect ratio.

I created a simple example, but I don’t know how to maintain aspect ratio.

Is it possible to set the minimum size while decreasing?

thank

HTML

<div id="image-container">
     <img src="Image.jpg" alt=""/>
</div>
<div id="image-controls">
     <input id="zoom-in" value="+" type="submit" />
     <input id="zoom-out" value="-" type="submit" />
</div>

JQuery

$(document).ready(function() {

var img = $('#image-container img');
var zoomWidthIncrement = img.width() * 1/3;
var zoomHeightIncrement = img.height() * 1/3;

$("#zoom-in").click(function (e){
    img.css({width: img.width() + zoomWidthIncrement, height: img.height() + zoomHeightIncrement});
e.preventDefault();
});

$("#zoom-out").click(function (e){
    img.css({width: img.width(function(i, w) { return w - zoomWidthIncrement; }), height: img.height(function(i, w) { return w - zoomWidthIncrement; })
});
e.preventDefault();
});
});
+3
source share
1 answer

Just change the width or height, not both. The browser will scale the image proportionally automatically.

JsFiddle example .

+1
source

All Articles