Computer >> Máy Tính >  >> Lập trình >> Javascript

Việc sử dụng sự kiện onscroll trong JavaScript là gì?

Sự kiện trên cuộn xảy ra khi thanh cuộn đang được cuộn cho một phần tử. Bạn có thể thử chạy mã sau để tìm hiểu cách triển khai onscroll sự kiện trong JavaScript.

Ví dụ

<!DOCTYPE html>
<html>
   <head>
      <style>
         div {
            border: 2px solid blue;
            width: 300px;
            height: 100px;
            overflow: scroll;
         }
      </style>
   </head>
   
   <body>
      <div id="content">
         This is demo text. This is demo text.This is demo text.This is demo text.
         This is demo text.This is demo text.This is demo text.This is demo text.
         This is demo text.This is demo text.This is demo text.
         This is demo text.This is demo text.This is demo text.
         This is demo text.This is demo text.This is demo text.
      </div>
      <p id = "myScroll"> </p>
      <script>
         document.getElementById("content").onscroll = function() {myFunction()};
         function myFunction() {
            document.getElementById("myScroll").innerHTML = "Scroll successfull!.";
         }
      </script>
   </body>
</html>