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.
I would use Bootstrap's ScrollSpy to do things like this.
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.