简体   繁体   中英

Scroll Website - I want my navigation div change color when scroll pass a section

I am not using any frameworks. How can I make my navigation div change color each time I scroll through a section on my page?

This is my web page: http://www.noteid.com/scl

As you can see there is a nav on the left and position: fixed; . How can I do this in Javascript?

It you need to use the scroll event.

window.onscroll = function() {
    var element = document.getElementById('testDiv');
    if (window.pageYOffset > 400 && window.pageYOffset < 800) 
        element.style.backgroundColor = 'blue';
};

This uses the pageYOffset property to set the background property.

http://jsfiddle.net/N82fS/9/

I would use Bootstrap's ScrollSpy to do things like this.

http://getbootstrap.com/javascript/#scrollspy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM