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
  • 741,623
  • 53
  • 500
  • 612
Thilanka De Silva
  • 1,088
  • 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,985
  • 19
  • 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
  • 2,013
  • 1
  • 12
  • 15