--- title: GlobalEventHandlers.onscroll slug: Web/API/GlobalEventHandlers/onscroll tags: - API - Event Handler - GlobalEventHandlers - HTML DOM - Property - Reference - イベントハンドラー - プロパティ translation_of: Web/API/GlobalEventHandlers/onscroll ---
onscroll は {{domxref("GlobalEventHandlers")}} ミックスインのプロパティで、 scroll イベントを処理するイベントハンドラー ({{event("Event_handlers", "event handler")}}) です。
scroll イベントは、ユーザー、 Web API、{{glossary("user agent","ユーザーエージェント")}}の何れかによって文書のビューまたは要素がスクロールしたときに発生します。
注: onscroll を {{domxref("GlobalEventHandlers.onwheel", "onwheel")}} と混同しないようにしてください。
onwheel は一般的なホイールの回転を扱うのに対し、 onscroll はオブジェクトの内容のスクロールを扱います。
target.onscroll = functionRef
functionRefonscroll ハンドラーは同時に1つだけ割り当てることができます。
柔軟性を高めるために、 {{event("scroll")}} イベントを {{domxref("EventTarget.addEventListener()")}} メソッドに渡すことができます。
この例では {{HtmlElement("textarea")}} のスクロールを監視し、その結果である要素の垂直のスクロール位置をログ出力します。
<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}`;
}
{{EmbedLiveSample("Example", 700, 200)}}
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName('HTML WHATWG','#handler-onscroll','onscroll')}} | {{Spec2('HTML WHATWG')}} | |
| {{SpecName("DOM3 Events", "#event-type-scroll", "onscroll")}} | {{Spec2("DOM3 Events")}} | 初回定義 |
{{Compat("api.GlobalEventHandlers.onscroll")}}