简体   繁体   English

window.onresize:在调整大小时以及调整大小时触发函数

[英]window.onresize: firing a function during, and when resizing is complete

I'm new to JavaScript. 我是JavaScript新手。 I am trying to figure out how I would create an onresize function to fire a function during and another one after the user is done resizing the window. 我试图弄清楚如何创建一个onresize函数来在用户调整窗口大小后以及在此过程中触发另一个函数。 Most likely a session timeout will occur. 会话超时很可能会发生。

Any ideas on writing a basic function? 关于编写基本功能有什么想法吗?

<script type="text/javascript">
    window.onresize = function () {
    // The first function goes here while resizing is happening
    // Then function 2 occurs when window resizing is finished by the user
    }    
</script>

Here is the modified version of what I am attempting. 这是我正在尝试的修改版本。 The goal is to hide the main capsule div on a page while the page is resizing. 目标是在页面调整大小时在页面上隐藏主胶囊div。 Initially when the onresize event is triggered is turns on a div's visibility to be a stand by screen saying "please wait while we process the information". 最初,在触发onresize事件时,将打开div的可见性,使其处于待机状态,并显示“请稍候,我们正在处理信息”。

The Script Exp: 脚本Exp:

var resizeTimeout;
window.onresize = function() {
        clearTimeout(resizeTimeout);
         document.getElementById("loading").className = "loading-visible";
         document.getElementById('XXXDIV').style.visibility = 'hidden';
             for ( var h = 1; h < 40; h++)
                 {
                 document.getElementById('DesignXXX' + h ).style.visibility = 'hidden';
                 }
    resizeTimeout = setTimeout(function() {
            var hideDiv = function(){document.getElementById("loading").className = "loading-invisible";};
            var oldLoad = window.onload;
            var newLoad = oldLoad ? function(){hideDiv.call(this);oldLoad.call(this);} : hideDiv;
            location.reload(true) = newLoad;
            document.getElementById('XXXDIV').style.visibility = 'visible';
             for ( var h = 1; h < 40; h++)
                 {
                 document.getElementById('DesignXXX' + h ).style.visibility = 'visible';
                 }
    }, 2000); // set for 1/4 second.  May need to be adjusted.
};

</script>

This should do it: 应该这样做:

var resizeTimeout;
window.onresize = function() {
        clearTimeout(resizeTimeout);
        // handle normal resize
        resizeTimeout = setTimeout(function() {
            // handle after finished resize
        }, 250); // set for 1/4 second.  May need to be adjusted.
    };

Here's a working example for doubters: http://pastehtml.com/view/b2jabrz48.html 这是怀疑者的有效示例: http : //pastehtml.com/view/b2jabrz48.html

On the jQuery website there is a comment to resize event: 在jQuery网站上,有一条注释可以调整事件大小:

Depending on implementation, resize events can be sent continuously as the resizing is in progress (the typical behavior in Internet Explorer and WebKit-based browsers such as Safari and Chrome), or only once at the end of the resize operation (the typical behavior in some other browsers such as Opera). 根据实现的不同,调整大小事件可以在调整大小过程中连续发送(Internet Explorer和基于WebKit的浏览器(例如Safari和Chrome)中的典型行为),也可以在调整大小操作结束时仅发送一次(调整中的典型行为)。其他一些浏览器,例如Opera)。

You're not going to get that event to fire while resizing is happening, because the event doesn't fire until after resizing has taken place. 您不会在调整大小时触发该事件,因为该事件直到调整大小后才会触发。

MDN Reference: MDN参考:

**NOTE:**

The resize event is fired after the window has been resized.

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

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