无法使用 document.getElementById() / ClassName() / TagName() / etc 设置“ul > li”元素的样式

问题描述 投票:0回答:2

function myFunction(e) {
  if (e.deltaY > 0) {
    let nav = document.getElementById('navbar').style;
    let navImg = document.getElementById('navBrand').style;

    nav.height = '6vh';
    nav.backgroundColor = '#00570e';
    nav.fontSize = '1vh';

    navImg.width = '4vh';
    navImg.height = '4vh';

  } else if (e.deltaY < 0) {

    document.getElementById('navbar').style = '';
    document.getElementById('navBrand').style = '';

  }
  document.getElementById("demo").innerHTML = "DeltaY value : " + e.deltaY;
}
body {
  height: 200vh;
  margin-top: 15vh;
}

#navbar {
  top: 0;
  left: 0;
  position: fixed;
  width: 100%;
  height: 10vh;
  background-color: green;
  transition: 0.3s ease;
  box-shadow: 0px 3px 15px #00000050;
}

#navbar>img {
  position: absolute;
  width: 40px;
  height: 40px;
  top: 1vh;
  left: 3vw;
  transition: 0.3s ease;
}

li {
  display: flex;
  margin-right: 3vw;
  color: white;
  font-family: Trebuchet MS;
  font-size: 1rem;
  float: right;
  text-align: left;
  transition: 0.3s ease;
}
<!DOCTYPE html>
<html>

<body onwheel="myFunction(event)">

  <div id="navbar">
    <img id="navBrand" src="https://rb.gy/0sjhmw">
    <ul>
      <li>Bottom</li>
      <li>Top</li>
      <li>Home</li>
    </ul>
  </div>

  <h1>Some Heading</h1>

  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </p>

  <br>
  <p id="demo"></p>

</body>
</html>

我正在尝试根据滚动方向(使用

e.deltaY
)缩小/扩展导航栏及其元素。

除了

ul > li
元素外,其他都很好。他们只是不想改变他们的
fontSize
color
或任何其他样式属性。

我已经尝试了标题中提到的一切;

document.getElementById()
document.getElementsByClassName()
document.getElementsByTagName()
//etc.

向上滚动时:

向下滚动时:

这是我达到的最大目标,但如果我能够shrink & expand the

fontSize
or alter styling the text as well!

javascript html getelementbyid getelementsbyclassname getelementsbytagname
2个回答
0
投票

问题是您试图通过父 div 元素导航栏的样式对象修改 ul > li 元素的 CSS 属性。但是样式对象只作用于元素的内联样式,不影响子元素的样式。

要修改ul > li 元素,可以使用querySelectorAll 方法选择所有li 元素,然后循环遍历它们以单独修改它们的样式。以下是如何修改代码中 li 元素的字体大小和颜色的示例:

function myFunction(e) {
  if(e.deltaY > 0){
    let nav = document.getElementById('navbar').style ;
    let navImg = document.getElementById('navBrand').style ;
    let lis = document.querySelectorAll('#navbar ul li');

    nav.height = '6vh';
    nav.backgroundColor = '#00570e';
    nav.fontSize = '1vh';

    navImg.width = '4vh';
    navImg.height = '4vh';
    
    for (let i = 0; i < lis.length; i++) {
      lis[i].style.fontSize = '0.8rem';
      lis[i].style.color = 'red';
    }
    
  } else if(e.deltaY < 0){
  
    document.getElementById('navbar').style = '' ;
    document.getElementById('navBrand').style = '' ;

    let lis = document.querySelectorAll('#navbar ul li');
    for (let i = 0; i < lis.length; i++) {
      lis[i].style.fontSize = '1rem';
      lis[i].style.color = 'white';
    }
  }
  document.getElementById("demo").innerHTML = "DeltaY value : " + e.deltaY;
}

此代码使用 querySelectorAll('#navbar ul li') 选择所有 li 元素,然后循环遍历它们以修改它们的字体大小和颜色样式。您可以修改这些样式以满足您的需要。请注意,您还可以使用 classList 属性在 li 元素中添加或删除类,然后在 CSS 中为这些类定义样式。这可以使您的代码更加模块化并且更易于管理。


-1
投票

你试过了吗

document.querySelectorAll('li').forEach(...)

© www.soinside.com 2019 - 2024. All rights reserved.