diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/globaleventhandlers/onwheel/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/globaleventhandlers/onwheel/index.html')
-rw-r--r-- | files/zh-cn/web/api/globaleventhandlers/onwheel/index.html | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/globaleventhandlers/onwheel/index.html b/files/zh-cn/web/api/globaleventhandlers/onwheel/index.html new file mode 100644 index 0000000000..b657f2f4b0 --- /dev/null +++ b/files/zh-cn/web/api/globaleventhandlers/onwheel/index.html @@ -0,0 +1,182 @@ +--- +title: GlobalEventHandlers.onwheel +slug: Web/API/GlobalEventHandlers/onwheel +tags: + - API + - DOM + - Event Handler + - GlobalEventHandlers + - Property + - Reference + - onwheel +translation_of: Web/API/GlobalEventHandlers/onwheel +--- +<p>{{ ApiRef("DOM") }}</p> + +<h2 id="归纳说明">归纳说明</h2> + +<p>{{domxref("GlobalEventHandlers")}} 的 <code>onwheel</code> 特性指向当前元素的滑轮滑动事件函数 {{domxref("EventHandler")}}。</p> + +<div class="blockIndicator note"> +<p><strong>注意:</strong>不要混淆 <code>onwheel</code><strong> </strong>和 {{domxref("GlobalEventHandlers.onscroll", "onscroll")}}:<code>onwheel</code> 通常用于处理滑轮的滚动事件,而 <code>onscroll</code> 用于处理某个对象内容的滚动。</p> +</div> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">target.onwheel = <em>functionRef;</em> +</pre> + +<h3 id="值">值</h3> + +<p><code>functionRef</code> 是一个函数名或者<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function">函数表达式</a>。该函数接受一个 {{domxref("WheelEvent")}} 对象作为唯一的传入参数。</p> + +<h2 id="举例">举例</h2> + +<p>以下例子展示了如何使用鼠标(或其它光标设备)的滚轮来缩放一个元素。</p> + +<h3 id="HTML">HTML</h3> + +<pre class="brush: html"><div>Scale me with your mouse wheel.</div></pre> + +<h3 id="CSS">CSS</h3> + +<pre class="brush: css">body { + min-height: 100vh; + margin: 0; + display: flex; + align-items: center; + justify-content: center; +} + +div { + width: 80px; + height: 80px; + background: #cdf; + padding: 5px; + transition: transform .3s; +}</pre> + +<h3 id="JavaScript">JavaScript</h3> + +<pre class="brush: js">function zoom(event) { + event.preventDefault(); + + if (event.deltaY < 0) { + // 放大 + scale *= event.deltaY * -2; + } + else { + // 缩小 + scale /= event.deltaY * 2; + } + + // 限制缩放 + scale = Math.min(Math.max(.125, scale), 4); + + // 应用缩放过渡效果 + el.style.transform = `scale(${scale})`; +} + +let scale = 1; +const el = document.querySelector('div'); +document.onwheel = zoom;</pre> + +<h3 id="结果">结果</h3> + +<p>{{EmbedLiveSample("Examples", 700, 400)}}</p> + +<h2 id="Specification" name="Specification">详情指南</h2> + +<table class="spectable standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG','webappapis.html#handler-onwheel','onwheel')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h3 id="注意">注意</h3> + +<p>当用户在相应元素上滚动滑轮便触发 {{ event("wheel") }} 事件。</p> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/wheel_event">Document: <code>wheel</code> event</a></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/wheel_event">Element: <code>wheel</code> event</a></li> +</ul> + +<p> + <audio style="display: none;"></audio> +</p> + +<p> + <audio style="display: none;"></audio> +</p> + +<p> + <audio style="display: none;"></audio> +</p> |