I'm having problems understanding why my toggleClass
inside of the function after the animate doesn't seem to trigger properly. When looking at console, you will be able to see that the element menu
is changing states, which leads me to think it is toggling both on and off quickly. Once the scroll animation completes, toggleClass
works as to be expected.
My script code is as follows:
$(document).ready(function() {
// initToggle();
stickyNav();
toggleMenu();
});
function stickyNav() {
var navOffset = $('.nav-bar').offset().top;
$(window).scroll(function() {
var nav = $('.nav-bar');
var scroll = $(window).scrollTop();
if (scroll >= navOffset) {
nav.addClass('fixed');
} else {
nav.removeClass('fixed');
}
});
}
function toggleMenu() {
$('.nav-menu').click(function() {
if (!$('.nav-bar').hasClass('fixed')) {
$('html, body').animate({scrollTop: $('.nav-bar').offset().top}, '800',
function() {
$('.menu').toggleClass('active');
$('body').toggleClass('no-scroll');
});
} else {
$('.menu').toggleClass('active');
$('body').toggleClass('no-scroll');
}
});
}
HERE is a "working" codepen of my code which better displays the issue I'm having.