window.onresize:在调整大小期间和完成时触发函数
我是 JavaScript 新手。我试图弄清楚如何创建一个 onresize 函数来在用户调整窗口大小后触发另一个函数。最有可能发生会话超时。
关于编写基本函数有什么想法吗?
<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>
这是我正在尝试的修改版本。目标是在调整页面大小时隐藏页面上的主胶囊 div。最初,当 onresize 事件被触发时,div 的可见性会变为备用屏幕,表示“我们正在处理信息,请稍候”。
脚本经验:
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>
I'm new to 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. 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. 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".
The Script 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该可以做到:
这是一个给怀疑者的工作示例: http://pastehtml.com/view/b2jabrz48.html
This should do it:
Here's a working example for doubters: http://pastehtml.com/view/b2jabrz48.html
在 jQuery 网站上有一条关于调整大小事件的评论:
On the jQuery website there is a comment to resize event:
在调整大小时,您不会触发该事件,因为直到调整大小之后该事件才会触发。
MDN 参考:
**注意:**
< code>调整大小事件在窗口大小调整后触发。
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:
**NOTE:**
The resize event is fired after the window has been resized.