Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我们在web的特定部分上滚动时,如何更改元素的样式?_Javascript_Html_Css - Fatal编程技术网

Javascript 当我们在web的特定部分上滚动时,如何更改元素的样式?

Javascript 当我们在web的特定部分上滚动时,如何更改元素的样式?,javascript,html,css,Javascript,Html,Css,我试图改变元素(菜单按钮)的背景颜色,当我们滚动到网站的相应部分,就像下面的网站一样 但我不知道如何设置“在特定div部分内滚动时”的条件 window.addEventListener('scroll',()=>{ if(this.pageYOffset

我试图改变元素(菜单按钮)的背景颜色,当我们滚动到网站的相应部分,就像下面的网站一样

但我不知道如何设置“在特定div部分内滚动时”的条件

window.addEventListener('scroll',()=>{
if(this.pageYOffset
到目前为止,我已经尝试了上面的内容,但是在我进入相应的行之前,样式就改变了。我错过了什么


另外,我尽量不使用jquery和其他JS库。

用下面的代码替换当前的
JavaScript
代码。通过消除代码重复和简化条件,我对它进行了一些清理。您必须记住的一点是,您必须根据顶部标题导航栏调整
偏移量。您可以使用
偏移量
并对其进行调整,以获得最佳的观看体验

var nav = document.querySelector('.nav');

function changeColor(aboutbutton, archivebutton, projectbutton, contactbutton) {
  document.getElementById("aboutbutton").style.backgroundColor = aboutbutton;
  document.getElementById("archivebutton").style.backgroundColor = archivebutton;
  document.getElementById("projectbutton").style.backgroundColor = projectbutton;
  document.getElementById("contactbutton").style.backgroundColor = contactbutton;
}

changeColor('gray', '#333', '#333', '#333');

var OFFSET = 90;

window.addEventListener('scroll', () => {

  if (this.pageYOffset > document.getElementById('fourthRow').offsetTop - OFFSET) {
    changeColor('#333', '#333', '#333', 'gray');
  } else if (this.pageYOffset > document.getElementById('thirdRow').offsetTop - OFFSET) {
    changeColor('#333', '#333', 'gray', '#333');
  } else if (this.pageYOffset > document.getElementById('secondRow').offsetTop - OFFSET) {
    changeColor('#333', 'gray', '#333', '#333');
  } else {
    changeColor('gray', '#333', '#333', '#333');
  }

})

function fixIfScrolled() {

  if (window.scrollY >= nav.offsetTop) {
    nav.classList.add("stationary");
  } else {
    nav.classList.remove("stationary");
  }

  if (window.scrollY == nav.offsetTop) {
    nav.classList.add("largerNavbar");

  } else {
    nav.classList.remove("largerNavbar");
  }

}

window.onscroll = function() {
  fixIfScrolled()
};

偏移量是导航条的高度吗?是的。再加上20像素。您可以调整这些值以获得最佳观看体验。
var nav = document.querySelector('.nav');

function changeColor(aboutbutton, archivebutton, projectbutton, contactbutton) {
  document.getElementById("aboutbutton").style.backgroundColor = aboutbutton;
  document.getElementById("archivebutton").style.backgroundColor = archivebutton;
  document.getElementById("projectbutton").style.backgroundColor = projectbutton;
  document.getElementById("contactbutton").style.backgroundColor = contactbutton;
}

changeColor('gray', '#333', '#333', '#333');

var OFFSET = 90;

window.addEventListener('scroll', () => {

  if (this.pageYOffset > document.getElementById('fourthRow').offsetTop - OFFSET) {
    changeColor('#333', '#333', '#333', 'gray');
  } else if (this.pageYOffset > document.getElementById('thirdRow').offsetTop - OFFSET) {
    changeColor('#333', '#333', 'gray', '#333');
  } else if (this.pageYOffset > document.getElementById('secondRow').offsetTop - OFFSET) {
    changeColor('#333', 'gray', '#333', '#333');
  } else {
    changeColor('gray', '#333', '#333', '#333');
  }

})

function fixIfScrolled() {

  if (window.scrollY >= nav.offsetTop) {
    nav.classList.add("stationary");
  } else {
    nav.classList.remove("stationary");
  }

  if (window.scrollY == nav.offsetTop) {
    nav.classList.add("largerNavbar");

  } else {
    nav.classList.remove("largerNavbar");
  }

}

window.onscroll = function() {
  fixIfScrolled()
};