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');
});
5
  • 2
    any fiddles to show? Commented Sep 11, 2014 at 6:24
  • if ($(window).scroll is... very strange. What really do want to check in if? Commented Sep 11, 2014 at 6:25
  • 1
    That if, and the braces, are completely out of whack. Commented Sep 11, 2014 at 6:28
  • $(window).scroll() binds a handler to the scroll event. It doesn't return anything that you can test with if. Commented Sep 11, 2014 at 6:28
  • possible duplicate of jQuery scroll() detect when user stops scrolling Commented Sep 11, 2014 at 6:31

2 Answers 2

1

Use Scroll and scrollstop

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

$(document).on("scrollstop",function(){
  $('#nav').css('opacity', '1');
});
Sign up to request clarification or add additional context in comments.

2 Comments

can you give the fiddle for the same
Sorry The above method is available in jquery mobile. But you can use the link mentioned in comment.
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) );

        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.