1

I have a top sticky menu bar. I add some css changes to it when the page starts to scroll past 1px. It works great right now, but I need it to change back when the scroll position is 0. Any help is appreciated. Thanks

Here is the js:

   $(document).scroll(function(){
if($(this).scrollTop() > 1)
{   
    $('.row-1').css({"box-shadow":"0 1px 3px rgba(0, 0, 0, 0.4)"});
 $('.row-1').css({"opacity":"0.9"});
}
});

2 Answers 2

7

CSS:

.row-1 { /* some styles for row 1 */ }
.row-1.scrolled { 
   box-shadow: 0 1px 3px rbga(0, 0, 0, 0.4);
   opacity: 0.9;
}

Then simply toggle that scrolling class:

 $(document).scroll(function(){
     $('.row-1').toggleClass('scrolled', $(this).scrollTop() > 1);
 });
Sign up to request clarification or add additional context in comments.

Comments

6

Try this :

$(document).scroll(function(){
    var row = $('.row-1'), scrollTop = $(this).scrollTop();
    if(scrollTop > 1)
    {   
        row .css({"box-shadow":"0 1px 3px rgba(0, 0, 0, 0.4)"});
        row .css({"opacity":"0.9"});
    }
    else if (scrollTop <= 1)
    {
        row.css({"box-shadow":"none"});
        row.css({"opacity":"1"});
    }
});

But you should use a css class and just toggle it. Like in the David Hedlund response.

1 Comment

+1 or you can define a css class, and then remove it or add it as needed.

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.