onscroll
特性为
GlobalEventHandlers
混合
EventHandler
处理
scroll
事件。
scroll
event fires when the document view or an element has been scrolled, whether by the user, a
Web API
,或
用户代理
.
注意:
Don't confuse
onscroll
with
onwheel
!
onwheel
handles general wheel rotation, while
onscroll
handles scrolling of an object's content.
target.onscroll = functionRef
functionRef
UIEvent
object as its sole argument.
Only one
onscroll
handler can be assigned to an object at a time.
For greater flexibility, you can pass a
scroll
event to the
EventTarget.addEventListener()
method instead.
This example monitors scrolling on a
<textarea>
, and logs the element's vertical scroll position accordingly.
<textarea>1 2 3 4 5 6 7 8 9</textarea> <p id="log"></p>
textarea {
width: 4rem;
height: 8rem;
font-size: 3rem;
}
const textarea = document.querySelector('textarea');
const log = document.getElementById('log');
textarea.onscroll = logScroll;
function logScroll(e) {
log.textContent = `Scroll position: ${e.target.scrollTop}`;
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'onscroll' in that specification. |
实时标准 | |
|
DOM (文档对象模型) 3 级事件规范
The definition of 'onscroll' in that specification. |
过时 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
onscroll
|
Chrome Yes | Edge ≤18 | Firefox Yes | IE ? | Opera ? | Safari ? | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android ? | Safari iOS ? | Samsung Internet Android Yes |
完整支持
兼容性未知
GlobalEventHandlers
onabort
onanimationcancel
onanimationend
onanimationiteration
onauxclick
onblur
oncancel
oncanplay
oncanplaythrough
onchange
onclick
onclose
oncontextmenu
oncuechange
ondblclick
ondurationchange
onended
onerror
onfocus
onformdata
ongotpointercapture
oninput
oninvalid
onkeydown
onkeypress
onkeyup
onload
onloadeddata
onloadedmetadata
onloadend
onloadstart
onlostpointercapture
onmousedown
onmouseenter
onmouseleave
onmousemove
onmouseout
onmouseover
onmouseup
onpause
onplay
onplaying
onpointercancel
onpointerdown
onpointerenter
onpointerleave
onpointermove
onpointerout
onpointerover
onpointerup
onreset
onresize
onscroll
onselect
onselectionchange
onselectstart
onsubmit
ontouchcancel
ontouchstart
ontransitioncancel
ontransitionend
onwheel
BeforeUnloadEvent
DOMStringMap
ErrorEvent
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers