当用户向上/向下滚动时隐藏/显示导航栏
这是我想要实现的示例:http: //haraldurthorleifsson.com/ 或 http://www.teehanlax.com/story/readability/
向下滚动时导航栏向上滑出屏幕,向上滚动时向下滑回屏幕。我已经想出了如何使用淡入/淡出来做到这一点,但我想用与示例中完全相同的动画来实现它。注意:我已经尝试过 SlideIn() 并且喜欢它执行拉伸动画的方式......
查询:
var previousScroll = 0,
headerOrgOffset = $('#header').offset().top;
$('#header-wrap').height($('#header').height());
$(window).scroll(function() {
var currentScroll = $(this).scrollTop();
console.log(currentScroll + " and " + previousScroll + " and " + headerOrgOffset);
if(currentScroll > headerOrgOffset) {
if (currentScroll > previousScroll) {
$('#header').fadeOut();
} else {
$('#header').fadeIn();
$('#header').addClass('fixed');
}
} else {
$('#header').removeClass('fixed');
}
previousScroll = currentScroll;
});
CSS:
#header {
width: 100%;
z-index: 100;
}
.fixed {
position: fixed;
top: 0;
}
#header-wrap {
position: relative;
}
HTML:
<div id="header-wrap">
<div id="header" class="clear">
<nav>
<h1>Prototype</h1>
</nav>
</div>
</div>