[英]toggle class when scrolling of page stops/idle
Basically I have this code that if you scroll down it hides the header menu then if you scroll up it displays the header menu again. 基本上,我有这段代码,如果您向下滚动它会隐藏标题菜单,然后如果向上滚动它会再次显示标题菜单。
What I need is hide the header menu when the scrolling becomes idle eg I scroll up header menu displays after 3 second of idle in scrolling it hides the header menu. 我需要的是在滚动变得空闲时隐藏标题菜单,例如,我在滚动空闲3秒后向上滚动标题菜单显示,它隐藏了标题菜单。
$(".slides li").hover(function () {
$(this).find('.flex-caption').css({opacity: 1.0});
},
function () {
$(this).find('.flex-caption').css({opacity: 0.0});
});
$('#nav-toggle').on('click', function(e) {
$(this).toggleClass("active");
$(".navmobile").slideToggle();
e.preventDefault();
});
$(document).ready(function(){
$(window).resize(function() {
if ($(window).width() >= 1303) {
$(".navmobile").hide();
$("#nav-toggle").removeClass("active");
}
else {
$("#nav-toggle").removeClass("active");
$(".navmobile").hide();
}
});
});
// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('.navbar').outerHeight();
$(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 0);
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
$('.navbar').removeClass('nav-down').addClass('nav-up');
$("#nav-toggle").removeClass("active");
$(".navmobile").hide();
} else {
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$('.navbar').removeClass('nav-up').addClass('nav-down');
} }
lastScrollTop = st;
}
Just set timeout of 3 seconds to remove some class or do other stuff: 只需将超时设置为3秒即可删除某些课程或执行其他操作:
var timer;
$(window).scroll(function(event){
didScroll = true;
clearTimeout(timer);
timer = setTimeout(function () {
$('#myHeader').removeClass('idle-scroll');
}, 3000);
});
Here clearTimeout
will stop previous timeout and starts new one. 在这里,
clearTimeout
将停止先前的超时并开始新的超时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.