我有这个脚本工作得很好,但由于某种原因,当向上滚动时,标题会在一秒钟之后重新启用。为什么?我无法理解。
以下是我的示例https://jsfiddle.net/wg8zv95f/
这是原始脚本http://jsfiddle.net/mariusc23/s6mLJ/31/
$(document).ready(function() {
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var headerContainer = $('#header');
var navbarHeight = headerContainer.outerHeight();
$(window).scroll(function(event) {
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
// Make sure they scroll more than delta
if (Math.abs(lastScrollTop - st) <= delta)
return;
// If they scrolled down and are past the navbar, add class .nav-up.
// This is necessary so you never see what is "behind" the navbar.
if (st > lastScrollTop && st > navbarHeight) {
// Scroll Down
headerContainer.removeClass('slideInDown').addClass('fadeOutUp');
} else {
// Scroll Up
if (st + $(window).height() < $(document).height()) {
headerContainer.removeClass('fadeOutUp').addClass('slideInDown');
}
}
lastScrollTop = st;
}
});
CSS
header {
position: fixed;
width: 100%;
animation-duration: 0.8s;
z-index: 99999;
}
答案 0 :(得分:1)
headerContainer.removeClass('slideInDown').addClass('fadeOutUp').slideUp();
headerContainer.removeClass('fadeOutUp').addClass('slideInDown').slideDown();
在脚本中添加
.slideInDown {
animation-name: slideInDown;
}
.fadeOutUp {
animation-name: fadeOutUp;
}
删除此stlyle