jquery~吸顶导航效果(移动端)

css:

	*{margin:0;padding:0;}
	body{
		margin:0 auto;
	    max-width:10rem;
	}
	header{
		width:10rem;
		height:1rem;
		background:red;
		position:fixed;
		top:0;
		left:auto;
	}
	section{
		height:100%;
		overflow: auto;
		padding:1rem 0;
	}
	.bananers{
		width:100%;
		height:3rem;
		text-align: center;
		line-height:3rem;
		background: aqua;
	}
	.mains{
		width:100%;
		height:1rem;
		background:red;
		display: flex;
	}
	.mains>div{
		width:100%;
		height:100%;
		border:1px solid #dddddd;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.contents{
		width:100%;
	}
	.contents>div{
		height:50px;
		line-height:50px;
		padding-left:10%;
		border-bottom:1px solid red;
	}
	footer{
		width:10rem;
		height:1rem;
		background: #0086B3;
		position:fixed;
		left:auto;
		bottom:0;
	}
    .fixed-top {
        position: fixed;
        width: 100%;
        top:1rem;
        left:auto;
    }
    .sticky {
        position: -webkit-sticky;/*滚过初始位置时自动吸顶*/
        position: sticky;/*吸顶时的定位*/
        top:1rem;
        left:auto;
        z-index: 999;/*z-index比下方所有层级要高*/
    }

html:

		<header>头部</header>
		<section>
			<div class="bananers">内容</div>
			<div class="mains">
				<div>导航1</div>
				<div>导航2</div>
				<div>导航3</div>
			</div>
			<div class="contents"></div>
		</section>
		<footer>页脚</footer>

js:

	for(var i=0;i<20;i++){
		$(".contents").append(`<div>${i+1}</div>`)
	}
	var headers=$("header")[0].getBoundingClientRect().height;
	var mains=$(".mains").offset().top;
	var heights=mains-headers;
	$(".bananers").html(mains+"----"+headers);
	var tops = document.querySelector('.mains');
	function fixed(num) {
        var nys= navigator.userAgent;
        var isAndroid = nys.indexOf('Android') > -1 || nys.indexOf('Adr') > -1; 
        var isIOS = !!nys.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); 
        if(isAndroid) {
            document.body.onscroll = function(e) {
                var scrollT = document.body.scrollTop;
                if (scrollT > num) {
                    $(tops).addClass('fixed-top');
                }else {
                    $(tops).removeClass('fixed-top');
                }
            };
        }else if(isIOS) {
            $(tops).addClass('sticky');
        }
    }
    fixed(heights);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值