在 HTML 中鼠标滚轮滚动到元素上时执行脚本?

htmlweb developmentfront end technology更新于 2026/1/17 9:16:17

当鼠标滚轮滚动到元素上时,onwheel 属性会触发。

示例

您可以尝试运行以下代码,在 HTML 中鼠标滚轮滚动到元素上时执行脚本 −

<!DOCTYPE html>
<html>
   <head>
   </head>
   <body>
      <div id = "myDIV" onwheel = "display()">This is demo text. Roll the mouse wheel here.</div>

      <script>
         function display() {
            alert("Mouse Wheel used!");
         }
      </script>

   </body>
</html>

相关文章


有用资源