在 JavaScript 中解释 Scroll 事件。


当用户通过向上或向下移动滚动条与滚动条交互时,JavaScript 会触发滚动事件。

以下是 JavaScript 中滚动事件的代码示例 −

示例

  在线演示

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      height: 150vh; /*To have a scrollbar*/
   }
   .result{
      font-size: 18px;
      color: blueviolet;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>Scroll events in JavaScript</h1>
<div class="result"></div>
<h3>Scroll the scrollbar to see some text in the above paragraph</h3>
<script>
   let resEle = document.querySelector(".result");
   window.addEventListener("scroll", () => {
      resEle.innerHTML = "The scroll event has fired";
   });
</script>
</body>
</html>

输出

向下滚动一点 −

更新于:16-7-2020

291 浏览量

开启您的 职业生涯

通过完成课程获得认证

立即开始
广告
© . All rights reserved.