我已设置以下代码来创建浮动导航菜单。它在Firefox中运行良好,但Chrome有一些问题。当我手动将条形滚动到它的阈值时,它会继续重置。
var nav = $$('#navigationBar');
var navCord = nav.getCoordinates();
var navHomeY = navCord[0].top;
var isFixed = false;
var $w = $(window);
$w.addEvent('scroll', function(){
var scrollTop = $w.getScroll().y;
console.log(scrollTop + ' and ' + navHomeY);
var shouldBeFixed = scrollTop > navHomeY;
if (shouldBeFixed && !isFixed){
console.log("fix it now");
nav.setStyles({
position: 'fixed',
top: 0,
left: navCord[0].left,
width: navCord[0].width
});
isFixed = true;
}
else if (!shouldBeFixed && isFixed)
{
nav.setStyles({
position: null,
top: null,
left: null,
width: null
});
isFixed = false;
}
});
});
以下是Chrome的console.log输出。
0 and 25 index.php:532
10 and 25 index.php:532
20 and 25 index.php:532
41 and 25 index.php:532
fix it now index.php:537
0 and 25 index.php:532 (PROBLEM: scrollTop reset happen)
这是Firefox的console.log输出。
9 and 25
17 and 25
26 and 25
fix it now
33 and 25
简而言之,在Firefox中,scrollTop表现得非常好,并且不会像在Chrome中一样重置为0。