Setting minimum size limit for a window minimization of browser?

后端 未结 4 897
迷失自我
迷失自我 2020-11-28 12:29

Is there a way to manually set the minimum size of a browser window across all browsers?

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 12:38

    Here's my solution for ensuring actual minimum content width and height, which includes dealing with different browser's "chrome" dimensions. This doesn't work with Firefox, but I've tested it in Edge and Chrome.

    function ensureMinimumWindowSize(width, height) {
        var tooThin = (width > window.innerWidth);
        var tooShort = (height > window.innerHeight);
    
        if (tooThin || tooShort) {
            var deltaWidth = window.outerWidth - window.innerWidth;
            var deltaHeight = window.outerHeight - window.innerHeight;
    
            width = tooThin ? width + deltaWidth : window.outerWidth;
            height = tooShort ? height + deltaHeight : window.outerHeight;
    
            // Edge not reporting window outer size correctly
            if (/Edge/i.test(navigator.userAgent)) {
                width -= 16;
                height -= 8;
            }
    
            window.resizeTo(width, height);
        }
    }
    
    var resizeTimer;
    window.addEventListener('resize', function(event) {
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(function () {
            ensureMinimumWindowSize(,);
        }, 250);
    }, false);
    

提交回复
热议问题