gpt4 book ai didi

jquery - 单击时增加/减小图像尺寸并保留纵横比

转载 作者:行者123 更新时间:2023-12-01 01:37:08 25 4
gpt4 key购买 nike

我希望能够增大(放大)和减小(缩小)图像并保持纵横比。

我创建了一个简单的示例,但不知道如何保持宽高比。

是否也可以在减小时设置最小尺寸?

谢谢

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();
});
});

最佳答案

仅更改宽度或高度,而不是同时更改两者。浏览器会自动按比例缩放图像。

<强> jsFiddle example

关于jquery - 单击时增加/减小图像尺寸并保留纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10377921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com