diff options
Diffstat (limited to 'files/zh-cn/web/api/window/resize_event/index.html')
| -rw-r--r-- | files/zh-cn/web/api/window/resize_event/index.html | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/window/resize_event/index.html b/files/zh-cn/web/api/window/resize_event/index.html new file mode 100644 index 0000000000..bdddef32e7 --- /dev/null +++ b/files/zh-cn/web/api/window/resize_event/index.html @@ -0,0 +1,182 @@ +--- +title: resize +slug: Web/API/Window/resize_event +translation_of: Web/API/Window/resize_event +--- +<p style="font-family: Microsoft Yahei;"><span class="seoSummary">文档视图调整大小时会触发 <strong>resize</strong> 事件。</span></p> + +<h2 id="基本信息" style="font-family: Microsoft Yahei;">基本信息</h2> + +<dl style="font-family: Microsoft Yahei;"> + <dt style="float: left; text-align: right; width: 120px;">规范</dt> + <dd style="margin: 0 0 0 120px;"><a class="external" href="http://www.w3.org/TR/DOM-Level-3-Events/#event-type-resize">DOM L3</a>, <a href="http://www.w3.org/TR/cssom-view/#resizing-viewports">CSSOM View</a></dd> + <dt style="float: left; text-align: right; width: 120px;">接口</dt> + <dd style="margin: 0 0 0 120px;">UIEvent</dd> + <dt style="float: left; text-align: right; width: 120px;">是否冒泡</dt> + <dd style="margin: 0 0 0 120px;">否</dd> + <dt style="float: left; text-align: right; width: 120px;">是否可取消默认</dt> + <dd style="margin: 0 0 0 120px;">否</dd> + <dt style="float: left; text-align: right; width: 120px;">事件目标</dt> + <dd style="margin: 0 0 0 120px;">defaultView (window)</dd> + <dt style="float: left; text-align: right; width: 120px;">默认行为</dt> + <dd style="margin: 0 0 0 120px;">None</dd> +</dl> + +<h2 id="属性" style="font-family: Microsoft Yahei;">属性</h2> + +<table class="standard-table" style="font-family: Microsoft Yahei;"> + <thead> + <tr> + <th scope="col">Property</th> + <th scope="col">Type</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>target</code> {{readonlyInline}}</td> + <td><a href="/en-US/docs/Web/API/EventTarget" title="EventTarget is an interface implemented by objects that can receive events and may have listeners for them."><code>EventTarget</code></a></td> + <td>The event target (the topmost target in the DOM tree).</td> + </tr> + <tr> + <td><code>type</code> {{readonlyInline}}</td> + <td><a href="/en-US/docs/Web/API/DOMString" title="DOMString is a UTF-16 String. As JavaScript already uses such strings, DOMString is mapped directly to a String."><code>DOMString</code></a></td> + <td>The type of event.</td> + </tr> + <tr> + <td><code>bubbles</code> {{readonlyInline}}</td> + <td><a href="/en-US/docs/Web/API/Boolean" title="The Boolean object is an object wrapper for a boolean value."><code>Boolean</code></a></td> + <td>Whether the event normally bubbles or not.</td> + </tr> + <tr> + <td><code>cancelable</code> {{readonlyInline}}</td> + <td><a href="/en-US/docs/Web/API/Boolean" title="The Boolean object is an object wrapper for a boolean value."><code>Boolean</code></a></td> + <td>Whether the event is cancellable or not.</td> + </tr> + <tr> + <td><code>view</code> {{readonlyInline}}</td> + <td><a class="new" href="/en-US/docs/Web/API/WindowProxy" rel="nofollow" title="The documentation about this has not yet been written; please consider contributing!"><code>WindowProxy</code></a></td> + <td><a href="/en-US/docs/Web/API/Document/defaultView" title="In browsers, document.defaultView returns the window object associated with a document, or null if none is available."><code>document.defaultView</code></a> (<code>window</code> of the document)</td> + </tr> + <tr> + <td><code>detail</code> {{readonlyInline}}</td> + <td><code>long</code> (<code>float</code>)</td> + <td>0.</td> + </tr> + </tbody> +</table> + +<h2 id="案例" style="font-family: Microsoft Yahei;">案例</h2> + +<p style="font-family: Microsoft Yahei;">由于resize事件可以以较高的速率触发, 因此事件处理程序不应该执行计算开销很大的操作 (如 DOM 修改)。相反, 建议使用<a href="/en-US/docs/DOM/window.requestAnimationFrame" title="/en-US/docs/DOM/window.requestAnimationFrame">requestAnimationFrame</a>, <a href="/en-US/docs/Web/API/WindowTimers/setTimeout">setTimeout</a> or <a href="/en-US/docs/Web/API/CustomEvent">customEvent</a>, 比如:</p> + +<h3 id="requestAnimationFrame_customEvent" style="font-family: Microsoft Yahei;">requestAnimationFrame + customEvent</h3> + +<pre class="brush: js">;(function() { + var throttle = function(type, name, obj) { + obj = obj || window; + var running = false; + var func = function() { + if (running) { return; } + running = true; + requestAnimationFrame(function() { + obj.dispatchEvent(new CustomEvent(name)); + running = false; + }); + }; + obj.addEventListener(type, func); + }; + + /* init - you can init any event */ + throttle("resize", "optimizedResize"); +})(); + +// handle event +window.addEventListener("optimizedResize", function() { + console.log("Resource conscious resize callback!"); +}); +</pre> + +<h3 id="requestAnimationFrame" style="font-family: Microsoft Yahei;">requestAnimationFrame</h3> + +<pre class="brush: js">var optimizedResize = (function() { + + var callbacks = [], + running = false; + + // fired on resize event + function resize() { + + if (!running) { + running = true; + + if (window.requestAnimationFrame) { + window.requestAnimationFrame(runCallbacks); + } else { + setTimeout(runCallbacks, 66); + } + } + + } + + // run the actual callbacks + function runCallbacks() { + + callbacks.forEach(function(callback) { + callback(); + }); + + running = false; + } + + // adds callback to loop + function addCallback(callback) { + + if (callback) { + callbacks.push(callback); + } + + } + + return { + // public method to add additional callback + add: function(callback) { + if (!callbacks.length) { + window.addEventListener('resize', resize); + } + addCallback(callback); + } + } +}()); + +// start process +optimizedResize.add(function() { + console.log('Resource conscious resize callback!') +}); +</pre> + +<h3 id="setTimeout" style="font-family: Microsoft Yahei;">setTimeout</h3> + +<pre class="brush: js">(function() { + + window.addEventListener("resize", resizeThrottler, false); + + var resizeTimeout; + function resizeThrottler() { + // ignore resize events as long as an actualResizeHandler execution is in the queue + if ( !resizeTimeout ) { + resizeTimeout = setTimeout(function() { + resizeTimeout = null; + actualResizeHandler(); + + // The actualResizeHandler will execute at a rate of 15fps + }, 66); + } + } + + function actualResizeHandler() { + // handle the resize event + ... + } + +}());</pre> |
