1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
---
title: GlobalEventHandlers.onscroll
slug: Web/API/GlobalEventHandlers/onscroll
tags:
- API
- HTML DOM
- 属性
translation_of: Web/API/GlobalEventHandlers/onscroll
---
<div>{{ ApiRef("HTML DOM") }}</div>
<p>元素的 <code>scroll </code>事件处理函数。</p>
<h3 id="语法">语法</h3>
<pre class="syntaxbox">element.onscroll = <var>functionReference</var></pre>
<h3 id="参数">参数</h3>
<p>functionReference 是一个函数的引用。当该元素滚动时,会执行该函数。</p>
<div class="blockIndicator note">
<p>注意:不要将onscroll 与 {{domxref("GlobalEventHandlers.onwheel", "onwheel")}}<font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">混淆。</span></font>onwheel<font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">是</span></font>鼠标滚轮旋转, 而onscroll 处理的是对象内部内容区的滚动事件。</p>
</div>
<p> </p>
<h2 id="示例">示例</h2>
<pre class="brush: html"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<style>
#container {
position: absolute;
height: auto;
top: 0;
bottom: 0;
width: auto;
left: 0;
right: 0;
overflow: auto;
}
#foo {
height:1000px;
width:1000px;
background-color: #777;
display: block;
}
</style>
</head>
<body>
<div id="container">
<div id="foo"></div>
</div>
<script type="text/javascript">
document.getElementById('container').onscroll = function() {
console.log("scrolling");
};
</script>
</body>
</html>
</pre>
<p>{{ EmbedLiveSample('示例') }}</p>
<h2 id="Example">Example</h2>
<p>这个示例能说明更多问题</p>
<p>This example monitors scrolling on a {{HtmlElement("textarea")}}, and logs the element's vertical scroll position accordingly.</p>
<h3 id="HTML">HTML</h3>
<pre class="brush: html"><textarea>1 2 3 4 5 6 7 8 9</textarea>
<p id="log"></p></pre>
<h3 id="CSS">CSS</h3>
<pre class="brush: css">textarea {
width: 4rem;
height: 8rem;
font-size: 3rem;
}</pre>
<h3 id="JavaScript">JavaScript</h3>
<pre class="brush: js">const textarea = document.querySelector('textarea');
const log = document.getElementById('log');
textarea.onscroll = logScroll;
function logScroll(e) {
log.textContent = `Scroll position: ${e.target.scrollTop}`;
}</pre>
<h3 id="Result">Result</h3>
<p>{{EmbedLiveSample("Example", 700, 200)}}</p>
<h2 id="注意">注意</h2>
<p>当用户滚动某个元素的内容时 <code>scroll</code> 事件将会被触发。<code>Element.onscroll 同等于</code> <code>element.addEventListener("scroll" ... )。</code></p>
<h2 id="规范">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("DOM3 Events", "#event-type-scroll", "onscroll")}}</td>
<td>{{Spec2("DOM3 Events")}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
|