I want to be able to increase (zoom in) and decrease (zoom out) an image and maintain the aspect ratio.
I have created a simple example but don’t know how to maintain the aspect ratio.
Is it also possible to set a minimum size when decreasing?
Thanks
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();
});
});
Just change the width or height, not both. The browser will scale the image proportionally automatically.
jsFiddle example.