按比例缩放图像以适合当前文档尺寸

时间:2012-09-17 01:57:01

标签: javascript jquery javascript-events resize image-resizing

我的图片尺寸为alfa x beta像素。

然后onDocumentReadyonWindowResize我计算当前的文档尺寸。

任务是按比例缩放图像,直到达到其中一个尺寸。

1 个答案:

答案 0 :(得分:2)

$(document).ready(function() {

    scaleStart();

});

$(window).resize(function() {

    scaleStart();

});

function scaleStart() {

    $("#myImage").css("min-width", 0);
    $("#myImage").css("min-height", 0);

    var originalWidth = 1762;
    var originalHeight = 1041;

    var safeWidth = $(document).width() - 100;
    var safeHeight = $(document).height() - 100;

    var scaleWidth = originalWidth / safeWidth;
    var scaleHeight = originalHeight / safeHeight;

    if (scaleWidth > scaleHeight)
    {
        $("#myImage").css("min-width", originalWidth / scaleWidth);
        $("#myImage").css("min-height", originalHeight / scaleWidth);
    }

    else
    {
        $("#myImage").css("min-width", originalWidth / scaleHeight);
        $("#myImage").css("min-height", originalHeight / scaleHeight);
    }

}