blob: af48e1575fef0c2ae109f76e97edcacd75a42825 (
plain)
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
|
---
title: window.onscroll
slug: Web/API/Window/onscroll
translation_of: Web/API/GlobalEventHandlers/onscroll
translation_of_original: Web/API/Window/onscroll
---
<p>{{ ApiRef() }}</p>
<h3 id="Summary" name="Summary">概述</h3>
<p>为当前页面的页面滚动事件添加事件处理函数.</p>
<h3 id="Syntax" name="Syntax">语法</h3>
<pre class="eval">window.onscroll = <em>funcRef</em>;
</pre>
<ul>
<li><code>funcRef</code> 是个函数类型的对象引用或者匿名函数.</li>
</ul>
<h3 id="Example" name="Example">例子</h3>
<pre class="brush: js">window.onscroll = function (e) {
// 当页面的滚动条滚动时,会执行这里的代码
}
</pre>
<pre class="brush: html"><html>
<head>
<title>onscroll test</title>
<script type="text/javascript">
window.onscroll = scroll;
function scroll()
{
alert("检测到页面滚动事件:"+window.pageXOffset+" "+window.pageYOffset);
// 注意: window.innerWidth 和 window.innerHeight 可以获得页面可见区域的宽和高.
}
</script>
</head>
<body>
<p>Resize the window</p>
<p>to a very small size,</p>
<p>and use the scrollbars</p>
<p>to move around the page content</p>
<p>in the window.</p>
</body>
</html>
</pre>
<h3 id="Notes" name="Notes">备注</h3>
<p>{{ Bug("189308") }}, 在旧版本的Gecko中(Gecko 1.8/Firefox 1.5之前), scroll 事件只会在用户拖动滚动条时发生,使用方向键和鼠标滚轮滚动页面则不会触发该事件.</p>
<p>当 window.scrollX/scrollY 不为 0时,意味着用户或者网页脚本已经执行了窗口的滚动行为.</p>
<h3 id="Specification" name="Specification">规范</h3>
<ul>
<li><a class="external" href="http://www.w3.org/TR/html5/webappapis.html#event-handlers-on-elements-document-objects-and-window-objects" title="http://www.w3.org/TR/html5/webappapis.html#event-handlers-on-elements-document-objects-and-window-objects">HTML5: Event handlers on elements, Document objects, and Window objects</a></li>
</ul>
<p>{{ languages( { "en": "en/DOM/window.onscroll"} ) }}</p>
|