简体   繁体   English

在主体上使用addEventListener进行滚动事件绑定不起作用。 为什么?

[英]Scroll event bind using addEventListener on body does not work. Why?

In the following, the javascript's scroll event does not call the function counter() when the page is scrolled. 在下面的内容中,当页面滚动时,javascript的scroll事件不会调用function counter() Why? 为什么?

JS JS

   $(function(){
            var body = document.getElementsByTagName('body');

            body[0].addEventListener('scroll',counter, true);
            var x = 0;
            function counter() {
                document.getElementById("demo").innerHTML = x += 1;
            }
        });

HTML HTML

<div id="demo"></div>
  • set height property to the document, to make the scroll event actually possible 为文档设置height属性,以使滚动事件实际上成为可能
  • body[0].addEventListener('scroll', counter, true) won't make any effects. body[0].addEventListener('scroll', counter, true)将不起作用。 body tag is the whole document - refer to the document instead body标签是整个文档-请参考该文档
  • it can be done without using jQuery 无需使用jQuery就可以完成

 (function() { document.addEventListener('scroll', counter, true); var x = 0; function counter() { document.getElementById("demo").innerHTML = x += 1; } })(); 
 body { height: 1000px; } #demo { position: fixed; } 
 <div id="demo"></div> 

Add the scroll event to the global window object and make sure that a scrollbar is actually visible so that the scroll event fires. 将滚动事件添加到全局窗口对象,并确保滚动条实际上​​是可见的,以便触发滚动事件。

Check the below code snippet. 检查以下代码段。

 $(function() { var x = 0; window.addEventListener('scroll', counter, true); function counter() { document.getElementById("demo").innerHTML = x += 1; } }); 
 body { height: 1000px; background-color: #E6E6E6; } #demo { position: fixed; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> <div id="demo"></div> 

Check this for more information 检查以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 addEventListener不适用于“ textarea”或“ body” - addEventListener does not work on “textarea” or “body” 为什么 document.body.addEventListener 事件在 document.addEventListener 之前运行? - Why does the document.body.addEventListener event run before document.addEventListener? 为什么 document.addEventListener 事件在 document.body.addEventListener 之后运行? - Why does the document.addEventListener event run after document.body.addEventListener? 为什么addEventListener不能用于Chrome中的单选按钮焦点事件? - Why does addEventListener not work for radio button's focus event in Chrome? addEventListener 对单击事件不起作用... - addEventListener does not work on click event… 使用此代码在游戏中循环播放音频,但不起作用。 为什么? - Using this code to loop audio in a game, but it does not work. Why? 附加的html上的preventDefault()不起作用。 为什么? - preventDefault( ) on appended html does not work. why? 画布上 ad​​dEventListener 的 onmousemove 事件不起作用 - The onmousemove event for addEventListener on canvas does not work jQuery的正则表达式选择器不起作用。 为什么? - Regex selector for jquery does not work. Why? 为什么 body.onfocus 触发而不是 body.addEventListener('focus')? - Why does body.onfocus trigger but not body.addEventListener('focus')?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM