2

So when I scroll down below 100px I want the menu to become transparent but when you scroll back to the top it goes back to solid color.

Example of my menu:

<div id="menu" class="transparent">
    <menu tags etc>
</div>

CSS

#menu {
background-color: black
}
.transparent {
opacity: 0.5
transition: all 0.5s
}

How can I do this using JavaScript? Not sure if I got this wrong already because my menu is already transparent due to the class transparent.

2 Answers 2

1

You need to write something like this:

$(document).scroll(function() {

  var scrollTop = $(window).scrollTop();
        if (scrollTop >= 200 ) {
            $('#menu').addClass("transparent");
        }
        else{
            $('#menu').removeClass("transparent");
        }
});

https://jsfiddle.net/jho5nnxj/

Sign up to request clarification or add additional context in comments.

2 Comments

I think my CSS or HTML might be wrong. Because it's just like how i wrote it up but my header is already transparent.
Remove the transparent class from the div just like this: <div id="menu" >
0

You can use scrollTop();

$(window).scroll(function() {

    var scroll = $('body').scrollTop();

      if (scroll >= 100) {
            $('.navbar').css('background', 'rgb(27, 17, 3)');
            anchor.css('color', '#fff');
        } else {
            // $('.navbar').css('background','rgba(246, 222, 166, 0.29)');
            $('.navbar').css('background', 'transparent');
            anchor.css('color', '#721001');

        }

});

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.