-3

I have been trying to achieve this during my learning process. I just started with javascript but everything is very blurry for me. How could i achieve this?enter image description here

here is the html

here is the css

Waalnut
  • 13
  • 1

2 Answers2

1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
window.onscroll = function (e) {  
// called when the window is scrolled.  
$("#myheader").css("background-color", "white");
}
</script>


<header id="myheader">This is my div.</div>
-1

In your header HTML add header class, where you need to change the background.

Add this jquery for adding and removing class in scroll.

$(window).scroll(function() {    
    var scroll = $(window).scrollTop();    
    if (scroll <= 500) { // change this 500 by your own need
        $(".header").addClass("lightHeader").removeClass("darkHeader");
    } else {
        $(".header").addClass("darkHeader").removeClass("lightHeader");
    }
}

Add this CSS:

.darkHeader {
   backgound-color: #000; //whatever color you want
}
.lightHeader {
   backgound-color: #fff; //whatever color you want
}