0

我正在使用window.open函数打开一个弹出窗口

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

现在我想要的是当用户尝试调整窗口大小时,应该保持纵横比,即如果宽度减小,那么相应的高度也应该减小,反之亦然。我只想计算新尺寸。到目前为止,我已经尝试过了

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

我知道那里有问题,因为我没有得到想要的结果。对此有任何解决方案吗?

4

2 回答 2

1

您需要将宽度调整为高度或反之亦然,而不是两者兼而有之。在这段代码中,我假设您希望将宽度调整为高度:

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);
}​

对于示例,我将原始宽度更改为 1000,但您可以在实际代码中将其改回。

于 2012-05-09T06:21:33.043 回答
0

您应该根据一个调整大小来保持纵横比。例如:

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 );

}

这样可以始终保持纵横比。

于 2012-05-09T06:21:05.883 回答