我找到了一个很棒的教程,可以在使用 Javascript ( http://code.stephenmorley.org/javascript/detachable-navigation/ ) 滚动时从页面中分离导航以使其保持静态。
但是,我想在多个导航 div 上实现这一点。
我假设它正在添加另一个类名, document.getElementById('navigation').className
但我无法获得正确的语法
这是代码:
/* Handles the page being scrolled by ensuring the navigation is always in
* view.*/
function handleScroll(){
// check that this is a relatively modern browser
if (window.XMLHttpRequest){
// determine the distance scrolled down the page
var offset = window.pageYOffset
? window.pageYOffset
: document.documentElement.scrollTop;
// set the appropriate class on the navigation
document.getElementById('navigation').className =
(offset > 104 ? 'fixed' : '');
}
}
// add the scroll event listener
if (window.addEventListener){
window.addEventListener('scroll', handleScroll, false);
}else{
window.attachEvent('onscroll', handleScroll);
}