zoukankan      html  css  js  c++  java
  • 仿淘宝滚动页面效果

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            body {
                background: rgba(151, 150, 150, .2);
            }
            
            div {
                width: 95%;
            }
            
            .topC {
                background: orange;
                height: 300px;
                width: 95%;
                margin-bottom: 10px;
            }
            
            .banner {
                background: plum;
                height: 300px;
                margin-bottom: 10px;
            }
            
            .main {
                background: darkcyan;
                height: 800px;
            }
            
            ul {
                position: absolute;
                list-style: none;
                margin: 0;
                padding: 0;
                top: 360px;
                right: 10px;
                background: white;
            }
            
            li {
                width: 40px;
                height: 40px;
                font-size: 14px;
                text-align: center;
                margin: 5px auto;
                border-top: rgb(214, 210, 210) 1px solid;
            }
            
            .top {
                display: none;
            }
        </style>
    </head>
    
    <body>
        <div class="topC"></div>
        <div class="banner"></div>
        <div class="main"></div>
        <ul>
            <li>实惠热卖</li>
            <li>淘宝特色</li>
            <li>猜你喜欢</li>
            <li class="top"><span>顶部</span></li>
            <li>反馈</li>
            <li>暴恐举报</li>
        </ul>
    
        <script>
            var ul = document.querySelector('ul');
            var topT = document.querySelector('.banner').offsetTop;
            var ulT = ul.offsetTop;
            var banT = ulT - topT;
            var mainT = document.querySelector('.main').offsetTop;
    
            document.addEventListener('scroll', function() {
                // 获取页面卷去部分 用 window.pageYOffset
                if (window.pageYOffset >= topT) {
                    ul.style.position = 'fixed';
                    ul.style.top = banT + 'px';
                } else {
                    ul.style.position = 'absolute';
                    ul.style.top = ulT + 'px';
                }
                var top = document.querySelector('.top');
                if (window.pageYOffset >= mainT) {
                    top.style.display = 'block';
                } else {
                    top.style.display = 'none';
                }
                // console.log(window.pageYOffset);
            })
        </script>
    </body>
    
    </html>
  • 相关阅读:
    js最佳实践
    js图片库 案例
    DOM
    js语法
    导航栏的ul中的li设置问题
    wDatepicker97的用法(点击事件联动)
    jqueyr validtion的使用
    哥哥写的例子(后台没有进行排序分组的时候,前台进行自己分组)
    关于require js加载的时候报错的问题
    一些简单的三目运算的使用(举例购物车)
  • 原文地址:https://www.cnblogs.com/qtbb/p/11706165.html
Copyright © 2011-2022 走看看