js滚动到指定位置导航栏固定顶部

window.onscroll=function(){
                 var topScroll =document.documentElement.scrollTop;//滚动的距离,距离顶部的距离
				 console.log(topScroll);
                 var bignav  = document.getElementById("bignav");//获取到导航栏id
                 if(topScroll > 200){  //当滚动距离大于250px时执行下面的东西
				 console.log(1111);
                     bignav.style.position = 'fixed';
                     bignav.style.top = '0';
                     bignav.style.zIndex = '9999';
                 }else{//当滚动距离小于250的时候执行下面的内容,也就是让导航栏恢复原状
                     bignav.style.position = 'static';
                 }
             }

<div id="bignav" class="tabSelect">

.tabSelect{
	display: flex;
	justify-content: space-around;
	height: 50px;
	width: 100%;
	background-color: white;
	line-height: 50px;
	font-size: 16px;
	padding: 0 10px;
	margin-bottom: 6px;
	border-bottom: 1px solid #efeff4;
}

猜你喜欢

转载自blog.csdn.net/qq_27064559/article/details/84623050