signed

QiShunwang

“诚信为本、客户至上”

某一指定位置的div在窗口滚动到指定位置的时候fixed定位

2021/6/3 16:08:48   来源:

目的:将附近机构一栏在滑动时固定为顶部的导航栏

直接上代码,亲测好用

<div class="navigation bg-white" style="z-index:990;width: 100%">
				    <div class="nearby pull-left" style="margin-left: 0.08rem;padding-top: 0.2rem;">附近机构</div>
				    <div class="pop pull-left" style="margin-left: 0.5rem;padding-top: 0.2rem;">热门套餐</div>
				    <div class="custom pull-left" style="margin-left: 0.5rem;padding-top: 0.2rem;">体检定制</div>
</div>
var oTop = $(".navigation").offset().top;//默认上边距离
	$(window).scroll(function(){
		if($(window).scrollTop() > oTop ){
			$(".navigation").css("position","fixed");
			$(".navigation").css("top","0");
		}else{
			$(".navigation").css("position","static")
		}
	});

效果演示:
滑动滚动条附近机构一栏固定