其他分享
首页 > 其他分享> > 跟随滚动条下拉菜单

跟随滚动条下拉菜单

作者:互联网

 
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        div:first-child {
            width: 100%;
            height: 200px;
            background: pink;
        }

        div:nth-child(2) {
            width: 100%;
            height: 400px;
            background: green;
        }

        div:nth-child(3) {
            width: 100%;
            height: 400px;
            background: blue;
        }

        div:nth-child(4) {
            width: 100%;
            height: 200px;
            background: red;
        }

        .box {
            background: peru;
            width: 20px;
            height: 50px;
            position: absolute;
            top: 200px;
            right: 0px;
        }

        .jay {
            position: relative;
        }
    </style>
</head>

<body>
    <div class="jay">
        <div>
            我是head
        </div>
        <div>
            我是banner
        </div>
        <div>
            我是Body
        </div>
        <div>

            我是footer
        </div>
        <div class="box">

        </div>
    </div>
    <script>
        var box = document.querySelector('.box')
        window.onscroll = function () {

            // console.log(document.documentElement.scrollTop);
            var flag = window.pageYOffset
            console.log(flag);
            if (flag >= 200) {
                box.style.position = 'fixed'
                console.log(111);
                box.style.top = '0px'
            } else {
                box.style.position = 'absolute'
                box.style.top = '200px'
            }
        }

    </script>
</body>

</html>

  

标签:box,跟随,style,height,滚动条,width,background,child,下拉菜单
来源: https://www.cnblogs.com/jayy520/p/15743466.html