定义和用法
onresize 事件会在窗口或框架被调整大小时发生。
语法
在 HTML 中:
<element onresize=”SomeJavaScriptCode“>
JavaScript 中:
window.onresize=function(){SomeJavaScriptCode};
| 参数 | 描述 |
|---|---|
| SomeJavaScriptCode | 必需。规定该事件发生时执行的 JavaScript。 |
所有主要浏览器都支持 onresize 事件
以下 HTMl 标签支持 onresize事件:
<a>, <address>, <b>, <big>, <blockquote>, <body>, <button>, <cite>, <code>, <dd>, <dfn>, <div>, <dl>, <dt>, <em>, <fieldset>, <form>, <frame>, <h1> to <h6>, <hr>, <i>, <img>, <input>, <kbd>, <label>, <legend>, <li>, <object>, <ol>, <p>, <pre>, <samp>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <table>, <textarea>, <tt>, <ul>, <var>
实例
当浏览器被重置大小时执行Javascript代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程(web176.com)</title>
<script>
function myFunction(){
var w=window.outerWidth;
var h=window.outerHeight;
var txt="窗口大小: 宽度=" + w + ", 高度=" + h;
document.getElementById("demo").innerHTML=txt;
}
</script>
</head>
<body onresize="myFunction()">
<p>尝试调整浏览器的窗口</p>
<p id="demo"> </p>
<p>注意:该例子在IE8 或更早版本下可能不工作,IE8 或更早的版本不支持window对象的outerWidth/outerHeight属性</p>
</body>
</html>作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/3922.html
支付宝
微信