zoukankan      html  css  js  c++  java
  • js固定在顶部

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1" />
            <title></title>
            <style>
                *{margin: 0px;padding: 0px;}
                html{overflow: auto;position: relative;}
                .div1{ 100%;height: 3rem;background: #8A2BE2;}
                .div2{ 100%;height: 3rem;background: #D2691E;}
                .div3{ 100%;height: 4rem;background: #E5EECC;text-align: center;line-height: 4rem;}
                .div4{ 100%;height: 9rem;background: #FF0000;}
                .div5{ 100%;height: 9rem;background: #FFA500;}
            </style>
            
        </head>
        <body>
            <script>
                var iWidth=document.documentElement.getBoundingClientRect().width;
                iWidth=iWidth>540?540:iWidth;
                document.getElementsByTagName("html")[0].style.fontSize=iWidth/10+"px";
            </script>
            <div class="wrap">
                <div class="div1"></div>
                <div class="div2"></div>
                <div class="div3" id="fixed">固定</div>
                <div class="div4"></div>
                <div class="div5"></div>
            </div>
        </body>
        <script src="js/jquery-1.11.3.js"></script>
        <script>

        window.onload=function(){
            var fixedTop = document.getElementById('fixed').offsetTop;
            document.ontouchmove = function() {
                move();
            }
            
            window.onscroll=function(){
                move();
            }
            
            function move(){
                var t = document.documentElement.scrollTop || document.body.scrollTop;
                if (t >= fixedTop) {
                    document.getElementById('fixed').style.position = "fixed";
                    document.getElementById('fixed').style.top=0;
                } else {
                    document.getElementById('fixed').style.position = "static";
                };
                console.log(document.getElementById('fixed').offsetTop)
            }
        }
        </script>
    </html>

  • 相关阅读:
    Axure RP使用攻略--动态面板滑动效果(10)
    Axure RP使用攻略--带遮罩层的弹出框(9)
    Axure RP使用攻略--动态面板的用途(8)
    Axure RP使用攻略--入门级(七)之axure元件使用思路的补充
    Axure RP使用攻略--入门级(六)关于Axure rp触发事件中IF和ELSE IF的使用说明
    Axure RP使用攻略--入门级(五)系统函数与变量
    Axure RP使用攻略--入门级(四)条件生成器
    Axure RP使用攻略--入门级(三)元件的触发事件
    Altium Designer 14安装破解
    小米手环暴力拆解
  • 原文地址:https://www.cnblogs.com/xiongcui/p/4892433.html
Copyright © 2011-2022 走看看