简体   繁体   English

在调整大小时调整弹出窗口的高度和宽度,以保持宽高比

[英]Adjusting height & width of pop up window on resize to maintain the aspect ratio

I am opening a pop up window using window.open function 我正在使用window.open函数打开一个弹出窗口

window.open('some_page.htm','','width=950,height=500');

Now what I want is when user tries to resize the window, the aspect ratio should be maintained ie, if width is reduced then accordingly height should also get reduced and vice versa. 现在我想要的是当用户尝试调整窗口大小时,应保持纵横比,即,如果宽度减小,则高度也应减小,反之亦然。 I just want to calculate the new dimensions. 我只想计算新尺寸。 So far I have tried this 到目前为止,我已经尝试过了

function ResizeWindow()
{
    var iOrignalWidth = 950;
    var iOrignalHeight = 500;
    var iOuterHeight = window.outerHeight;
    var iOuterWidth = window.outerWidth;

    var iNewOuterWidth = Math.round((iOrignalWidth / iOrignalHeight) * iOuterHeight);
    var iNewOuterHeight = Math.round((iOrignalHeight / iOrignalWidth) * iNewOuterWidth);

    alert("New Width: "+ iNewOuterWidth + "\t" + "New Height" + iNewOuterHeight);
}

I know that there's something wrong up there since I am not getting desired results. 我知道那里有些问题,因为我没有得到理想的结果。 ANy solution on this ? 可以解决这个问题吗?

You'll want to either adjust the width to the height or visa versa, not both. 您可能想要将宽度调整为高度,反之亦然,而不是两者都调整。 In this code, I assumed you want the width adjusted to the height: 在此代码中,我假设您要将宽度调整为高度:

function ResizeWindow()
{
    var iOrignalWidth = 1000;
    var iOrignalHeight = 500;
    var iOrginalRatio = iOrignalWidth/iOrignalHeight; // 2

    var iOuterWidth = window.outerWidth; // example: 1083
    var iOuterHeight = window.outerHeight; //example: 600

    var iNewOuterHeight = iOuterHeight; // 600
    var iNewOuterWidth = Math.round(iNewOuterHeight*iOrginalRatio); //600 * 2 = 1200

    alert("New Width: "+ iNewOuterWidth + "\t" + "New Height" + iNewOuterHeight);
}​

I changed to original width to 1000 for the example, but you can change that back in your actual code. 我将示例的原始宽度更改为1000,但是您可以在实际代码中将其更改为原来的宽度。

You should do to accoording to one resize for maintain the aspect ratio. 您应该按照一种调整大小的方式来保持纵横比。 For example: 例如:

function ResizeWindow()
{
    var iOrignalWidth = 950;
    var iOrignalHeight = 500;
    var iOuterHeight = window.outerHeight; 
    var iOuterWidth = window.outerWidth;

    var w = (window.outerWidth  - iOrignalWidth) / iOrignalWidth; // for exam: (1280-950) / 950= 0.34
    var h = (window.outerHeight - iOrignalHeight) / iOrignalHeight; // for exam : (800 - 500) / 500= 0.60

    var newWidth;
    var newHeight;
    if (w<h)
    {
        // If percentage of width is less than percentage of height, Resize should be according to percentage of width.
        newWidth = iOrignalWidth * w * 100;
        newHeight = iOrignalHeight * w *100;
    }
    else
    {
        // If percentage of height is less than  percentage of width, Resize should be according to percentage of height.
        newWidth = iOrignalWidth * h * 100;
        newHeight = iOrignalHeight * h *100;
    }

    alert("New Width: "+ newWidth + "\t" + "New Height" + newHeight );

}

So that maintain the aspect ratio is always preserved. 这样始终保持长宽比不变。

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

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