Home Web Front-end H5 Tutorial The event onscroll that is triggered when the element scroll bar is scrolling in html5

The event onscroll that is triggered when the element scroll bar is scrolling in html5

Nov 07, 2017 am 09:50 AM
h5 html5 trigger

Example

Executed when the element scrollsJavaScript
<div onscroll="myFunction()">
Copy after login

Definition and usage

onscroll event is executed when the element scroll bar is scrolling trigger.

Tip: Use the CSS overflow style property to create scroll bars for elements.

Browser support

The event onscroll that is triggered when the element scroll bar is scrolling in html5

Syntax

HTML:

<element onscroll="myScript">
Copy after login

JavaScript:

object.onscroll=function(){myScript};
Copy after login

JavaScript, use the addEventListener() method:

object.addEventListener("scroll", myScript);
Copy after login

Note: Internet Explorer 8 and earlier IE versions do not support the addEventListener() method.

Technical Details

Technical Details

## Supported HTML tags:
,
, ,
Whether bubbling is supported:Yes
Can be canceled:NO
Event type:Event
,
,
, ,
,
,
,
,
,

-

, ,
  • , , ,
      ,

      ,

      , <select>, <tbody>, <textarea>, <tfoot>, <thead>, <ul><td width="938" valign="top" style="word-break: break-all;"></td></tr></tbody><p style="margin: 10px 0px; padding: 0px; font-size: 16px; line-height: 1.5; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);">onscroll事件失效</p><pre class='brush:php;toolbar:false;'><!DOCTYPE html>
      <html>
      <head>
            <meta charset="utf-8">
      </head>
      <body onscroll="checkscroll()">
            <div class="father" >
                  <div class="son">我是移动块</div>
            </div>
      </body>
      </html>
      <style type="text/css">
      html,
      body {
            width: 100%;
            height: 100%;
            /*overflow-x:hidden;*/
            /*overflow: scroll;*/
      }
      .father {
            width: 100%;
            height: 100%;
            box-sizing: border-box;
      }
      .son {  
        height: 3000px;
            background-color: yellow;
            width: 100%;
            font-size: 80px;
            display: flex;
            justify-content: center;
            align-items: center;
      }
      </style>
      <script type="text/javascript">
        function checkscroll() {  
              console.info(window.scrollY)
        }
      </script>
      Copy after login

      大家可以将这段代码copy亲自测试一下,在有和没有overflow属性之间切换,就明白了。

      因为很多同学在开发项目时,会有脚手架之类的文件进行快速开发,但是有的脚手架会在入口的html文件,设置overflow: scroll/auto。

      这个属性在入口级别的文件中最好不要随意使用,造成的scroll滑动监听整体失效,你无论如何也不会想到是这个原因。

      打个比方:

      APP.vue

      The event onscroll that is triggered when the element scroll bar is scrolling in html5

      这是用vue.js写的页面,我在其中用了vux(一个基于vuejs的移动组件库)的一个组件view-box,当时我找遍了所有的文件,并没有那里

      有over-flow 样式;但是scroll事件失效,window.scrollY一直为0, 最后我发现所有页面都是如此,于是我就定位在入口的几个文件,将

      相关的引入组件也进行排查,终于在view-box,这个组建里找到了over-flow:auto;样式。注释掉后,就正常了

      所以说:over-flow:auto;如果放在入口文件并且放在包裹的父元素上,一定要慎重!

      The above is the detailed content of The event onscroll that is triggered when the element scroll bar is scrolling in html5. For more information, please follow other related articles on the PHP Chinese website!

      Statement of this Website
      The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

      Hot AI Tools

      Undresser.AI Undress

      Undresser.AI Undress

      AI-powered app for creating realistic nude photos

      AI Clothes Remover

      AI Clothes Remover

      Online AI tool for removing clothes from photos.

      Undress AI Tool

      Undress AI Tool

      Undress images for free

      Clothoff.io

      Clothoff.io

      AI clothes remover

      Video Face Swap

      Video Face Swap

      Swap faces in any video effortlessly with our completely free AI face swap tool!

      Hot Tools

      Notepad++7.3.1

      Notepad++7.3.1

      Easy-to-use and free code editor

      SublimeText3 Chinese version

      SublimeText3 Chinese version

      Chinese version, very easy to use

      Zend Studio 13.0.1

      Zend Studio 13.0.1

      Powerful PHP integrated development environment

      Dreamweaver CS6

      Dreamweaver CS6

      Visual web development tools

      SublimeText3 Mac version

      SublimeText3 Mac version

      God-level code editing software (SublimeText3)

      Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

      Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

      Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

      This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

      HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

      Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

      HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

      Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

      HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

      Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

      HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

      Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

      HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

      Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

      Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

      Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

      See all articles