onscroll 事件

事件对象参考手册 事件对象

<div> 元素滚动时执行 JavaScript

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>蜜蜂教程(mifengjc.com)</title>
  <style>
    div {
      border: 1px solid black;
      width: 200px;
      height: 100px;
      overflow: scroll;
    }
  </style>
</head>
<body>

  <p>尝试滚动 div。</p>
  <div onscroll="myFunction()">In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
    <br><br> 'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'</div>
  <p>滚动 <span id="demo">0</span> 次。</p>
  <script>
    x = 0;

    function myFunction() {
      document.getElementById("demo").innerHTML = x += 1;
    }
  </script>

</body>
</html>

尝试一下 »


定义和用法

onscroll 事件在元素滚动条在滚动时触发。

提示: 使用 CSS overflow 样式属性来创建元素的滚动条。


浏览器支持

Event
onscroll Yes Yes Yes Yes Yes

语法

HTML 中:

<element onscroll="myScript">
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>蜜蜂教程(mifengjc.com)</title>
  <style>
    div {
      border: 1px solid black;
      width: 200px;
      height: 100px;
      overflow: scroll;
    }
  </style>
</head>
<body>

  <p>尝试滚动 div。</p>
  <div onscroll="myFunction()">In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
    <br><br> 'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'</div>
  <p id="demo"></p>
  <script>
    function myFunction() {
      document.getElementById("demo").innerHTML = "div在滚动";
    }
  </script>

</body>
</html>

尝试一下 »

JavaScript 中:

object.onscroll = function() {
  myScript
};
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>蜜蜂教程(mifengjc.com)</title>
  <style>
    div {
      border: 1px solid black;
      width: 200px;
      height: 100px;
      overflow: scroll;
    }
  </style>
</head>
<body>

  <p>该实例演示了如何使用 HTML DOM 向 div 元素中添加 "onscroll" 事件。</p>
  <p>尝试滚动 div</p>
  <div id="myDIV">In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
    <br><br> 'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'</div>
  <p id="demo"></p>
  <script>
    document.getElementById("myDIV").onscroll = function() {
      myFunction()
    };

    function myFunction() {
      document.getElementById("demo").innerHTML = "你滚动了 div。";
    }
  </script>

</body>
</html>

尝试一下 »

JavaScript 中, 使用 addEventListener() 方法:

object.addEventListener("scroll", myScript);
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>蜜蜂教程(mifengjc.com)</title>
  <style>
    div {
      border: 1px solid black;
      width: 200px;
      height: 100px;
      overflow: scroll;
    }
  </style>
</head>
<body>

  <p>该实例演示了如何使用 addEventListener() 方法向 div 元素中添加 "onscroll" 事件。</p>
  <p>尝试滚动 div</p>
  <div id="myDIV">In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
    <br><br> 'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'</div>
  <p id="demo"></p>
  <script>
    document.getElementById("myDIV").addEventListener("scroll", myFunction);

    function myFunction() {
      document.getElementById("demo").innerHTML = "你滚动了 div。";
    }
  </script>

</body>
</html>

尝试一下 »

注意: Internet Explorer 8 及更早 IE 版本不支持 addEventListener() 方法。


Technical Details

技术细节

是否支持冒泡: Yes
是否可以取消: No
事件类型: Event
支持的 HTML 标签: <address>, <blockquote>, <body>, <caption>, <center>, <dd>, <dir>, <div>, <dl>, <dt>, <fieldset>, <form>, <h1> - <h6>, <html>, <li>, <menu>, <object>, <ol>, <p>, <pre>, <select>, <tbody>, <textarea>, <tfoot>, <thead>, <ul>

事件对象参考手册 事件对象