美文网首页
js中获取首屏的宽高和滚动距离监听

js中获取首屏的宽高和滚动距离监听

作者: SmallTwo | 来源:发表于2017-04-18 13:38 被阅读126次
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            #content{
                width: 100%;
                height: 2000px;
                background: -webkit-gradient(linear,0 0, 0 1500,from(yellow),to(blue));
            }
            #btn {
                display: none;
                position: fixed;
                right: 100px;
                bottom: 100px;
            }
        </style>
    </head>
    <body>
    
    <div id="content">
        <input type="button" value="点击" id="btn">
    </div>
    <script>
        var btn = document.getElementById('btn'),content = document.getElementById('content');
        window.onscroll = function () {
            // 获取窗口滚动的距离
            var scrolDis = document.documentElement.scrollTop || document.body.scrollTop;
            // 获取首屏窗口的高度
            var firHei = document.documentElement.clientHeight || document.body.clientHeight;
    
           btn.style.display =  scrolDis > firHei ? 'block' : 'none';
        };
    </script>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:js中获取首屏的宽高和滚动距离监听

          本文链接:https://www.haomeiwen.com/subject/ivbfzttx.html