Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

window.onscroll

概述

为当前页面的页面滚动事件添加事件处理函数.

语法

window.onscroll = funcRef;
  • funcRef 是个函数类型的对象引用或者匿名函数.

例子

window.onscroll = function (e) {
  // 当页面的滚动条滚动时,会执行这里的代码
}
<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>

备注

bug 189308, 在旧版本的Gecko中(Gecko 1.8/Firefox 1.5之前), scroll 事件只会在用户拖动滚动条时发生,使用方向键和鼠标滚轮滚动页面则不会触发该事件.

当 window.scrollX/scrollY 不为 0时,意味着用户或者网页脚本已经执行了窗口的滚动行为.

规范

文档标签和贡献者

 此页面的贡献者: teoli, khalid32, ziyunfei
 最后编辑者: khalid32,