zoukankan      html  css  js  c++  java
  • scroll 事件究竟是发生在那个元素上的?

    测试的浏览器有 1.  chrome : Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36
            2. safari: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7

            3. Firefox: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:59.0) Gecko/20100101 Firefox/59.0
    代码如下:

    <!DOCTYPE html>
    
    <html>
    
    <head>
    <meta charset="UTF-8">
    </head>
    <style>
    .scroll-container {
    width: 200px;
    border: 10px solid blue;
    height: 300px;
    overflow: scroll;
    }
    
    .scroll-container>div {
    height: 1000px;
    background-color: burlywood;
    }
    </style>
    
    <body>
    <div class="scroll-container" id="scrollContainer">
    <div id="scrollChild">
    1 2 3
    </div>
    </div>
    </body>
    <script>
    window.onload = function () {
    document.getElementById('scrollChild').onscroll = function () {
    console.log('scrollChild scroll?')
    }
    document.getElementById("scrollContainer").onscroll = function () {
    console.log('scrollContainer scroll?')
    }
    }
    </script>
    
    </html>

    结果都是在控制台打印出  scrollContainer scroll?

    why? 为什么事件的监听需要在外侧包裹元素(父元素)上?并且scrollTop等值在child Element 上面都是零,在父元素上才是有值的~
    在MDN文档上面
     
    并没有特别说明需要在父元素监听scroll事件,说明理应打印的是scrollChild scroll?
    待续。。。
  • 相关阅读:
    uva 816
    SQL语言类
    VTK中国文字显示和简单的医疗图像浏览软件
    系统开始了解--感兴趣的文章
    的天数,以一个日期与当前日期的
    POJ 2528 QAQ段树+分离
    IT该忍者神龟Instant client required
    进程和线程之间的通信
    2014——我的求职(两)
    如何绕过chrome的弹窗拦截机制
  • 原文地址:https://www.cnblogs.com/www-wwr/p/9039202.html
Copyright © 2011-2022 走看看