JS 浏览器BOM-->onscroll方法

1.定义和用法

  当窗口发生滚动会触发该事件

  语法:

    window.onscroll=function(){myScript};

    参数:myScript:为窗口发生滚动会触发的事件

  举例:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>js_excise</title>
        <script src="./js/js_excise.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body style="height: 187.5rem;">
        <script type="text/javascript">
            window.onscroll=function(){
                //打印滚动高度
                var scroll_height = document.documentElement.scrollTop || document.body.scrollTop
                console.log(scroll_height)
            }
        </script>
    </body>
</html>

  输出:

原文地址:https://www.cnblogs.com/abner-pan/p/12715845.html