0

i want to change CSS or menu bar while scrolling and restore old CSS when scroll stops using if else. this is what i did so far.

if ($(window).scroll(function () {
    $('#nav').css('opacity', '0.85');
} else {
    $('#nav').css('opacity', '1');
});

working code without if statement

$(document).scroll(function() {
    $('#nav').css('opacity', '0.85');
});
Barmar
  • 669,327
  • 51
  • 454
  • 560
Thilanka De Silva
  • 1,060
  • 13
  • 15

2 Answers2

1

Use Scroll and scrollstop

$(document).on("scroll", function() {
  $('#nav').css('opacity', '0.85');
});

$(document).on("scrollstop",function(){
  $('#nav').css('opacity', '1');
});
jbmyid
  • 1,935
  • 17
  • 22
1

Try this

 scrolling = "Scrolling",
 stopped = "Stopped";

        $( window ).scroll(function() {
            console.log(scrolling);
            clearTimeout( $.data( this, "scrollCheck" ) );
            $.data( this, "scrollCheck", setTimeout(function() {
                console.log( stopped );
            }, 250) );

        });
Harutyun Abgaryan
  • 1,963
  • 1
  • 11
  • 15