简体   繁体   English

滚动网站-当滚动部分通过时,我希望导航div更改颜色

[英]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? 每次滚动页面上的某个部分时,如何使导航div更改颜色?

This is my web page: http://www.noteid.com/scl 这是我的网页: http : //www.noteid.com/scl

As you can see there is a nav on the left and position: fixed; 如您所见,左侧有导航栏, position: fixed; . How can I do this in Javascript? 如何使用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. 这使用pageYOffset属性设置background属性。

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

I would use Bootstrap's ScrollSpy to do things like this. 我会用Bootstrap的ScrollSpy来做这样的事情。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我滚动时,我的导航栏颜色不想随 jquery 改变? - My nav bar color doesn't want to change with my jquery when I scroll? 我的主页上有这个“div”,希望它在我滚动时随我移动,顺便说一句,我正在为网站使用 react - I have this `div` on my home page and want it to move with me when I scroll, I am using react for the website btw 我想通过单击箭头导航使每个部分水平滚动?每个部分都有无限的垂直滚动 - i want to make each section to scroll horizontally by clicking an arrow navigation?each section has infinite vertical scroll 在网站的不同 div 上滚动时使 Bootstrap4 导航栏改变颜色 - Make Bootstrap4 Navigation bar change colour when scroll on different div of the website 滚动通过每个部分时更改SVG的颜色 - Change color of SVG when scroll past each section 当我们在 a 上滚动时更改线性渐变背景颜色<section></section> - Change linear-gradient background color when we scroll on a <section> 在滚动时更改 div 背景颜色 - Change div background color on scroll 当我只想滚动一个部分时整个页面滚动 - Entire page scrolling when I only want one section to scroll 滚动时,jQuery导航菜单不会更改颜色 - JQuery navigation menu doesn't change color when scroll 如何让 jQuery 在滚动时更改导航颜色? - How can I make jQuery change navigation color on scroll?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM