我有一个函数用于确定图像在特定矩形内的绝对最大尺寸(同时保留其纵横比)。我目前使用的代码肯定是错误的,但我不确定我出错的地方。确定图像可以在矩形内的最大可能尺寸
// Sets the image to the largest proportional size possible
// based on the current browser window dimensions and original
// image size specified by the image loader
function applyLargestProportionalSize() {
var maxWidth = <width of container>;
var maxHeight = <height of container>;
var realWidth = <actual image width>;
var realHeight = <actual image height>;
if (realWidth < realHeight && maxWidth > maxHeight) {
var scaledWidth = Math.min(realWidth, maxWidth);
$('img').css('width', scaledWidth);
// let height be determined by the browser
} else {
var scaledHeight = Math.min(realHeight, maxHeight);
$('img').css('height', scaledHeight);
// let width be determined by the browser
}
}
您如何知道代码错误?它出什么问题了? – 2012-02-14 23:55:35