为什么window.onresize会触发好几次?

fmjwn 2004-08-09 03:43:11
代码如下:
<table width="95%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000000">
<tr>
<td><span id="objSpan"></span></td>
</tr>
</table>
<script language="javascript">
function test()
{
alert(objSpan.parentElement.offsetWidth);
}
window.onresize = test;
</script>
有时我改变一次窗体大小会触发好几次onresize事件。
为什么?
...全文
1167 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
blestcc 2004-08-10
  • 打赏
  • 举报
回复
我测试的时候只改变宽度也触发两次
fmjwn 2004-08-10
  • 打赏
  • 举报
回复
我用ie6.0有时4次。如何判断是改变Width而触发的?
zerochang 2004-08-10
  • 打赏
  • 举报
回复
大家看看这个,MSDN上的原文地址如下:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_window.asp

onresize Event Internet Development Index

--------------------------------------------------------------------------------

Fires when the size of the object is about to change.

Syntax

Inline HTML <ELEMENT onresize = "handler" ... > All platforms
Event property object.onresize = handler JScript only
object.onresize = GetRef("handler") Visual Basic Scripting Edition (VBScript) 5.0 or later only
Named script <SCRIPT FOR = object EVENT = onresize> Internet Explorer only

Event Information

Bubbles No
Cancels No
To invoke Change the height or width of the object.
Default action No default action.

Event Object Properties

Although event handlers in the DHTML Object Model do not receive parameters directly, a handler can query an event object for data.

Show Available Properties

altKey Sets or retrieves a value that indicates the state of the ALT key.
altLeft Sets or retrieves a value that indicates the state of the left ALT key.
clientX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
clientY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the client area of the window, excluding window decorations and scroll bars.
ctrlKey Sets or retrieves the state of the CTRL key.
ctrlLeft Sets or retrieves the state of the left CTRL key.
offsetX Sets or retrieves the x-coordinate of the mouse pointer's position relative to the object firing the event.
offsetY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the object firing the event.
returnValue Sets or retrieves the return value from the event.
screenX Retrieves the x-coordinate of the mouse pointer's position relative to the user's screen.
screenY Sets or retrieves the y-coordinate of the mouse pointer's position relative to the user's screen.
shiftKey Sets or retrieves the state of the SHIFT key.
shiftLeft Retrieves the state of the left SHIFT key.
srcElement Sets or retrieves the object that fired the event.
type Sets or retrieves the event name from the event object.
x Sets or retrieves the x-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.
y Sets or retrieves the y-coordinate, in pixels, of the mouse pointer's position relative to a relatively positioned parent element.

Remarks

The onresize event fires for block and inline objects with layout, even if document or CSS (cascading style sheets) property values are changed. Objects have layout when measurements such as the height and width attributes are set, or when the position of the object is set. Intrinsic objects, such as button, and windowed objects, such as window and iframe, fire as expected. This event does not fire for files with embedded controls.

Resizing HTML applications also fires the onresize event.

Standards Information

There is no public standard that applies to this event.
cuixiping 2004-08-10
  • 打赏
  • 举报
回复
onresize的触发是这样的:按照一个很短的间隔来判断,当你把200*200的窗口拖到200*400的时候中间要经过几次resize,从而触发几个onresize.
要避免多次onresize触发引起某个代码执行多次的不良后果,可以自己写个函数,在onresize之后间隔一个时间值(setTimeout)再调用你要执行的代码,通过置状态变量判断是否要执行,如果两次onresize的时间间隔小于100ms,就再setTimeout,等等看。
BainStudio 2004-08-09
  • 打赏
  • 举报
回复
同一楼的
blestcc 2004-08-09
  • 打赏
  • 举报
回复
测了半天没弄懂,关注
woyingjie 2004-08-09
  • 打赏
  • 举报
回复
两次
fastson 2004-08-09
  • 打赏
  • 举报
回复
一个窗体的width、height、top、left,任何一个属性变更都会触发onresize事件

87,901

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧