我找到了这个教程 http://benweiser.com/add-a-sticky-top-bar-widget-to-genesis-that-hides-and-reveals-on-scroll/并喜欢这个想法。
jQuery(document).ready(function($) {
// Hide Top bar on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var topbarHeight = $('.top-bar').outerHeight();
$(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
if(Math.abs(lastScrollTop - st) <= delta)
return;
if (st > lastScrollTop && st > topbarHeight){
// Scroll Down
$('.top-bar').removeClass('bar-down').addClass('bar-up');
} else {
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$('.top-bar').removeClass('bar-up').addClass('bar-down');
}
}
lastScrollTop = st;
}
});
我想知道是否有一种方法可以在用户向上滚动时隐藏栏,但在他们向下滚动时显示栏。
我有兴趣在上面放一些分享按钮,当用户向上滚动时显示这些按钮不会很有效。
最佳答案
我不明白这个问题,因为在 if-else block 中代表:
// Scroll Down
和
// Scroll Up
因此,当您想要在用户向下滚动时添加内容时,将您的代码放在 IF 中;如果用户向上滚动时,则将代码放在 ELSE 中。
在这种情况下:交换IF和ELSE的代码。
关于jquery - 当用户向下滚动而不是向上滚动时显示 css?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33948989/