<script type="text/javascript">
$(function() {
$("#originalWidth").val($("#img1").width());
$("#originalHeight").val($("#img1").height());
var originalWidth = $("#originalWidth").val();
var originalHeight = $("#originalHeight").val();
var desireWidth = $("#desireWidth").val();
var desireHeight = $("#desireHeight").val();
var scaledWidth = 0, scaledHeight = 0;
var wRate = 1, hRate = 1, setRate = 1;
$("#btn").click(function() {
if (originalHeight > desireHeight) {
hRate = desireHeight / originalHeight;
}
if (originalWidth > desireWidth) {
wRate = desireWidth / originalWidth;
}
if (wRate != 1 || hRate != 1) {
if (wRate > hRate) {
setRate = hRate;
}
else {
setRate = wRate;
}
}
scaledWidth = originalWidth * setRate;
scaledHeight = originalHeight * setRate;
$("#scaledWidth").val(scaledWidth);
$("#scaledHeight").val(scaledHeight);
$("#img1").width(scaledWidth);
$("#img1").height(scaledHeight);
});
});
$(function() {
$("#originalWidth").val($("#img1").width());
$("#originalHeight").val($("#img1").height());
var originalWidth = $("#originalWidth").val();
var originalHeight = $("#originalHeight").val();
var desireWidth = $("#desireWidth").val();
var desireHeight = $("#desireHeight").val();
var scaledWidth = 0, scaledHeight = 0;
var wRate = 1, hRate = 1, setRate = 1;
$("#btn").click(function() {
if (originalHeight > desireHeight) {
hRate = desireHeight / originalHeight;
}
if (originalWidth > desireWidth) {
wRate = desireWidth / originalWidth;
}
if (wRate != 1 || hRate != 1) {
if (wRate > hRate) {
setRate = hRate;
}
else {
setRate = wRate;
}
}
scaledWidth = originalWidth * setRate;
scaledHeight = originalHeight * setRate;
$("#scaledWidth").val(scaledWidth);
$("#scaledHeight").val(scaledHeight);
$("#img1").width(scaledWidth);
$("#img1").height(scaledHeight);
});
});
</script>
本文转自博客园张占岭(仓储大叔)的博客,原文链接:图像的等比例缩放,如需转载请自行联系原博主。