onwheel 特性为 GlobalEventHandlers 混合 EventHandler 处理 wheel 事件。

wheel event fires when the user rotates the mouse (or other pointing device) wheel.

注意: Don't confuse onwheel with onscroll : onwheel handles general wheel rotation, while onscroll handles scrolling of an object's content.

句法

target.onwheel = functionRef;
					

functionRef is a function name or a 函数表达式 . The function receives a WheelEvent object as its sole argument.

范例

This example shows how to scale an element using the mouse (or other pointing device) wheel.

HTML

<div>Scale me with your mouse wheel.</div>
					

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;
}
					

JavaScript

function zoom(event) {
  event.preventDefault();
  if (event.deltaY < 0) {
    // Zoom in
    scale *= event.deltaY * -2;
  }
  else {
    // Zoom out
    scale /= event.deltaY * 2;
  }
  // Restrict scale
  scale = Math.min(Math.max(.125, scale), 4);
  // Apply scale transform
  el.style.transform = `scale(${scale})`;
}
let scale = 1;
const el = document.querySelector('div');
document.onwheel = zoom;
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'onwheel' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
onwheel Chrome 61 Edge 12 Firefox Yes IE Yes Opera 48 Safari Yes WebView Android 61 Chrome Android 61 Firefox Android Yes Opera Android 45 Safari iOS Yes Samsung Internet Android 8.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改:
  1. DOM (文档对象模型)
  2. GlobalEventHandlers
  3. 特性
    1. onabort
    2. onanimationcancel
    3. onanimationend
    4. onanimationiteration
    5. onauxclick
    6. onblur
    7. oncancel
    8. oncanplay
    9. oncanplaythrough
    10. onchange
    11. onclick
    12. onclose
    13. oncontextmenu
    14. oncuechange
    15. ondblclick
    16. ondurationchange
    17. onended
    18. onerror
    19. onfocus
    20. onformdata
    21. ongotpointercapture
    22. oninput
    23. oninvalid
    24. onkeydown
    25. onkeypress
    26. onkeyup
    27. onload
    28. onloadeddata
    29. onloadedmetadata
    30. onloadend
    31. onloadstart
    32. onlostpointercapture
    33. onmousedown
    34. onmouseenter
    35. onmouseleave
    36. onmousemove
    37. onmouseout
    38. onmouseover
    39. onmouseup
    40. onpause
    41. onplay
    42. onplaying
    43. onpointercancel
    44. onpointerdown
    45. onpointerenter
    46. onpointerleave
    47. onpointermove
    48. onpointerout
    49. onpointerover
    50. onpointerup
    51. onreset
    52. onresize
    53. onscroll
    54. onselect
    55. onselectionchange
    56. onselectstart
    57. onsubmit
    58. ontouchcancel
    59. ontouchstart
    60. ontransitioncancel
    61. ontransitionend
    62. onwheel
  4. 实现通过:
    1. Document
    2. HTMLElement
    3. SVGElement
    4. Window
    5. XULElement
  5. DOM 相关页面
    1. AbortController
    2. AbortSignal
    3. AbstractRange
    4. Attr
    5. ByteString
    6. CDATASection
    7. CSSPrimitiveValue
    8. CSSValue
    9. CSSValueList
    10. CharacterData
    11. ChildNode
    12. 注释
    13. CustomEvent
    14. DOMConfiguration
    15. DOMError
    16. DOMErrorHandler
    17. DOMException
    18. DOMImplementation
    19. DOMImplementationList
    20. DOMImplementationRegistry
    21. DOMImplementationSource
    22. DOMLocator
    23. DOMObject
    24. DOMParser
    25. DOMPoint
    26. DOMPointInit
    27. DOMPointReadOnly
    28. DOMRect
    29. DOMString
    30. DOMTimeStamp
    31. DOMTokenList
    32. DOMUserData
    33. Document
    34. DocumentFragment
    35. DocumentType
    36. 元素
    37. ElementTraversal
    38. Entity
    39. EntityReference
    40. 事件
    41. EventTarget
    42. HTMLCollection
    43. MutationObserver
    44. 节点
    45. NodeFilter
    46. NodeIterator
    47. NodeList
    48. NonDocumentTypeChildNode
    49. ProcessingInstruction
    50. PromiseResolver
    51. 范围
    52. StaticRange
    53. 文本
    54. TextDecoder
    55. TextEncoder
    56. TimeRanges
    57. TreeWalker
    58. TypeInfo
    59. USVString
    60. UserDataHandler
    61. XMLDocument