Bazinga777
Bazinga777

Reputation: 5281

jquery scroll function working only once

I have the following code that is supposed to detect the scroll position and change classes accordingly but for some reason it is working only once.

    $(document).on('scroll',function() { 
   // Do something  
   var scroll = $(window).scrollTop();
  if (scroll >= 70)    
   {      
     if ($('#head-nav').hasClass('off') ) {     
           $('#head-nav').removeClass('top clearfix off');                     
           $('#head-nav').addClass('top clearfix on');     
           $('#cart').removeClass('cart-on');
           $('#cart').addClass('cart-off'); 
           $('#nav-menu').removeClass('navigation grid-40 pull-30 tablet-grid-25 tablet-pull-25 omega alpha');
           $('#nav-menu').addClass('navigation grid-40 pull-0 tablet-grid-25 tablet-pull-25 omega alpha');
           //$('#cart').css('dislay','none !important');
       }
   }
  else if(scroll < 70)
    {
    if ($('#head-nav').hasClass('on') ) {
           $('#cart').removeClass('cart-off');
           $('#cart').addClass('cart-on');            
           $('#head-nav').removeClass('top clearfix on');
           $('#head-nav').removeClass('top clearfix off');  
           $('#nav-menu').removeClass('navigation grid-40 pull-0 tablet-grid-25 tablet-pull-25 omega alpha');
           $('#nav-menu').addClass('navigation grid-40 pull-30 tablet-grid-25 tablet-pull-25 omega alpha');
       }
  }
      });

You can check my code here

Upvotes: 0

Views: 156

Answers (2)

Jorge Sainz
Jorge Sainz

Reputation: 133

In the case scroll<70 you're removing the class off (and on) at the same time from #head-nav so the second time you test the classes, you don't find neither on nor off

Upvotes: 1

Sergei Zahharenko
Sergei Zahharenko

Reputation: 1524

Scroll function working properly... you do not add class off back to $('#head-nav')... i guess error in this line:

    $('#head-nav').removeClass('top clearfix on');
    $('#head-nav').removeClass('top clearfix off');  //? add class not remove?

Upvotes: 1

Related Questions