예
window.onscroll=function(){
var topScroll = get_scrollTop_of_body();//滚动的距离,距离顶部的距离
var bignav = document.getElementById("navbar");//获取到导航栏id
if(topScroll > 250){ //当滚动距离大于250px时执行下面的东西
bignav.style.position = 'fixed';
bignav.style.top = '0';
//bignav.style.zIndex = '-1';
}else{//当滚动距离小于250的时候执行下面的内容,也就是让导航栏恢复原状
bignav.style.position = 'static';
}
}
/*解决浏览器兼容问题*/
function get_scrollTop_of_body(){
var scrollTop;
if(typeof window.pageYOffset != 'undefined'){//pageYOffset指的是滚动条顶部到网页顶部的距离
scrollTop = window.pageYOffset;
}else if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
scrollTop = document.documentElement.scrollTop;
}else if(typeof document.body != 'undefined'){
scrollTop = document.body.scrollTop;
}
return scrollTop;
}
매우 상세한, 그것은 설명하지 않습니다. 거리를 스크롤링하는 수단 (250) 및 페이지 섹션 코드를 실행, 250 픽셀을 초과하는 것을 특징으로하는 본 실시. 0 즉시 구현 될 수 있습니다.
데이터
- 참조 webxy