简体   繁体   English

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

[英]Scale image proportionally to fit current document dimensions

I have an image with some dimension alfa x beta pixels.我有一个尺寸为alfa x beta像素的图像。

Then onDocumentReady and onWindowResize I calculate current document dimensions.然后onDocumentReadyonWindowResize我计算当前文档尺寸。

Task is to scale image proportionally until one of dimensions is reached.任务是按比例缩放图像,直到达到其中一个维度。

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

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM