Detect browser window Maximized/Minimized with Javascript

徘徊边缘 提交于 2020-01-10 04:57:04

问题


When the user clicks the Maximized/Restore Down or Minimised buttons of the browser, is there a way to keep track of these events with javascript? Are there any appropriate event or property to use?

I want that when the user clicks the 'maximized' button at the top-right of the browser, the webpage should stretch to a specific width and when the browser is in the resizable mode, the webpage is resizable.

Please can someone help me in this? I don't mind either javascript or jquery.

Thanks in advance.


回答1:


It sounds like what you want is a layout that resizes when the browser is resized but only up to a maximum width.

If that's the case you can either do it in CSS or in JavaScript.

Imagining that your content is in an element like:

<div class="container"> -content here- </div>

My preferred option would be to use CSS as follows:

.container{
    max-width: 1200px;
    width: 100%;
}

If you need to support IE6 (which doesn't support max-width) you can try using an expression in the IE6 CSS:

.container{
    width:expression(document.body.clientWidth > 1200 ? "1200px" : "100%" ); /*IE6*/
}

If you want to use JavaScript: you could just resize your elements in the window's resize event:

$(window).bind('resize', function() {
    // resize $('.container').width() based on $(window).width()
});

You could initially trigger that logic using

$(window).trigger('resize');


来源:https://stackoverflow.com/questions/3371973/detect-browser-window-maximized-minimized-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!