The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Add/remove class by jquery

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
  • #186554

    Hey, guys, here is the code:

    $(document).ready(function() {
    $(window).scroll(function() {
    if ($(window).scrollTop() > 800) {
    $( ‘.header-menu’).addClass(‘shadow-menu’);
    } else {

    And the issue: when I move back to top the class ‘shadow-menu’ appears again. Why this happens?


    Can you create a reduced case in so we can see what happens exactly?

    Its a bit messy, but as you can see: when its ‘else’ in code every class works exectly right, but ‘shadow-menu’ still appears when you scroll to top and when you on very top too. why its happening?


    Poor knowledge, thats the only reason.
    I was expected the main thing: the delay. Script adds classes and in a moment later it adds the “shaddow” class. As far as I know, I can put .setTimeout? but I dont understand how… yet… Can you help me with it?


    setTimeout is a native javascript function, not a jQuery method. This means you couldn’t chain it with your other jQuery methods:

    setTimeout( function(){
        $( ‘.header-menu’).addClass(‘shadow-menu’);

    But I suspect that you could simply put .addClass after .delay (i.e., without using .queue/.dequeue at all).

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.