簡體   English   中英

按比例縮放圖像以適合當前文檔尺寸

[英]Scale image proportionally to fit current document dimensions

我有一個尺寸為alfa x beta像素的圖像。

然后onDocumentReadyonWindowResize我計算當前文檔尺寸。

任務是按比例縮放圖像,直到達到其中一個維度。

$(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