How do you resize a browser window so that the inner width is a specific value

后端 未结 1 549
半阙折子戏
半阙折子戏 2020-12-25 13:50

If you are just here to tell me on why resizing a window is a bad idea, obnoxious, and the web-equivalent of nails on a chalk board, then please go away. I am looking for a

相关标签:
1条回答
  • 2020-12-25 14:08

    The post is quite old, but here is a concrete implementation for future readers :

    var resizeViewPort = function(width, height) {
        if (window.outerWidth) {
            window.resizeTo(
                width + (window.outerWidth - window.innerWidth),
                height + (window.outerHeight - window.innerHeight)
            );
        } else {
            window.resizeTo(500, 500);
            window.resizeTo(
                width + (500 - document.body.offsetWidth),
                height + (500 - document.body.offsetHeight)
            );
        }
    };
    

    And if you want to take the scrollbars into account :

    var resizeViewPort = function(width, height) {
        var tmp = document.documentElement.style.overflow;
        document.documentElement.style.overflow = "scroll";
    
        if (window.outerWidth) {
            window.resizeTo(
                width + (window.outerWidth - document.documentElement.clientWidth),
                height + (window.outerHeight - document.documentElement.clientHeight)
            );
        } else {
            window.resizeTo(500, 500);
            window.resizeTo(
                width + (500 - document.documentElement.clientWidth),
                height + (500 - document.documentElement.clientHeight)
            );
        }
    
        document.documentElement.style.overflow = tmp;
    };
    

    Have fun...

    0 讨论(0)
提交回复
热议问题