What I'm trying to do is basically, I have a nav bar with a logo in it. When a specific link with an ID of #theMenu is clicked a class of "moveMainLogo" should be added to the logo image and a class of "open" should be added to the div with class "navContainer".
BUT if the user scrolls down at all, I'd like the the "moveMainLogo" class added straight away and then if #theMenu is clicked the only thing that then happens is that .open class is added to the div with class "navContainer".
Then if the user scrolls back to the top of the page it reverts back to the original behavious or adding and removing both classes when #theMenu is clicked.
So I think I'm essentially trying to do this...
IF (distance to top is greater than 0)
apply class A to Element A
When Element B is clicked, apply class B to Element B
ELSE
When Element B is clicked, apply class B to Element B AND apply class A to Element A
This is the code I've tried so far, which kind of works as I wanted, the problem being that it seems to toggle the classes not only when the distance the user has scrolled is greater than 0 but EVERY time the user scrolls any amount wherever they are on the page.
window.onscroll = function() {myFunction()};
function myFunction() {
if (document.body.scrollTop > 0 || document.documentElement.scrollTop > 0) {
$(".mainLogo").toggleClass("moveMainLogo");
$("#theMenu").click(function () {
$("#animateMenu").toggleClass("hamburger-slim-clicked");
$(".navContainer").toggleClass("open");
});
} else {
$("#theMenu").click(function () {
$("#animateMenu").toggleClass("hamburger-slim-clicked");
$(".navContainer").toggleClass("open");
});
$("#theMenu").click(function () {
$(".mainLogo").toggleClass("moveMainLogo");
});
}
}
EDIT:
Fiddle: https://jsfiddle.net/2h3x0uam/1/
Before the user scrolls, if they click menu, movemainlogo class should apply. If they close the menu, the movemainLogo class should be removed.
If they scroll, the movemainmenu class should apply. If they open the menu after scrolling, movemainlogo class should remain applied. If they scroll back to the top, movemainlogo class should be removed.