getelementsbyclass用于所有href链接标记

at0kjp5o  于 2021-09-23  发布在  Java
关注(0)|答案(2)|浏览(240)

目前,我正在尝试使用javascript更改字体颜色 <a> 标签它需要发生在你向下滚动时,颜色从白色变为黑色,但我一直在努力,当你向下滚动时,它只会改变一个 <a> 标记为黑色,其余保持白色。
以下是我的导航的简短代码:

<div class="rc_nav" id="centered_nav">
        <a href="#home" class="link1">Home</a>
        <a href="#services" class="link1">Services</a>
        <a href="#about" class="link1">About</a>
        <a href="#contact" class="link1">Contact</a>
        <a href="javascript:void(0);" title="Menu" style="font-size:18px;" class="icon" onclick="myFunction()">&#9776;</a>
    </div>

这就是我的javascript的样子

const nav = document.getElementById('centered_nav');
const logo = document.getElementById('rc_logo');
const link = document.getElementsByClassName('link1')[0];

window.onscroll = function(){

    if (window.pageYOffset > 100) {

        nav.style.background = "#FFFFFF";
        nav.style.boxShadow = "0px 2px 1px #E55200";
        link.style.color = "#000000";
        logo.style.backgroundImage = "url('http://localhost/assets/img/nav-logo-color.png')";
    }
    else {
        nav.style.background = "transparent";
        nav.style.boxShadow = "none";
        link.style.color = "#FFFFFF";
        logo.style.backgroundImage = "url('http://localhost/assets/img/nav-logo.png')";
    }
}

我做了一些研究,因为我不太熟悉javascript,但没有任何结果。有什么想法吗?

sbtkgmzw

sbtkgmzw1#

你可以用 document.querySelectorAll 获取所有锚元素并在其上循环以设置颜色(注意 document.querySelectorAll 优先于 document.getElementsByClassName 因为返回的集合不是活动的。)

const nav = document.getElementById('centered_nav');
const logo = document.getElementById('rc_logo');
const links = document.querySelectorAll('.link1');

window.onscroll = function(){

    if (window.pageYOffset > 100) {

        nav.style.background = "#FFFFFF";
        nav.style.boxShadow = "0px 2px 1px #E55200";
        links.forEach(link => link.style.color = "#000000");
        logo.style.backgroundImage = "url('http://localhost/assets/img/nav-logo-color.png')";
    }
    else {
        nav.style.background = "transparent";
        nav.style.boxShadow = "none";
        links.forEach(link => link.style.color = "#FFFFFF");
        logo.style.backgroundImage = "url('http://localhost/assets/img/nav-logo.png')";
    }
}
eqqqjvef

eqqqjvef2#

直接的答案是您影响了许多链接,而不是一个链接,因此您需要创建一个集合并使用类似的方法对其进行迭代 forEach . 在您使用的代码中 document.getElementsByClassName('link1')[0]; 这只是第一个环节。

const nav = document.getElementById('centered_nav');
const logo = document.getElementById('rc_logo');
const links = document.getElementsByClassName('link1');

window.onscroll = function(){
    if (window.pageYOffset > 100) {
        nav.style.background = "#FFFFFF";
        nav.style.boxShadow = "0px 2px 1px #E55200";
        links.forEach(link => link.style.color = "#000000" );
        logo.style.backgroundImage = "url('http://localhost/assets/img/nav-logo-color.png')";
    }
    else {
        nav.style.background = "transparent";
        nav.style.boxShadow = "none";
        links.forEach(link => link.style.color = "#ffffff" );
        logo.style.backgroundImage = "url('http://localhost/assets/img/nav-logo.png')";
    }
}

更好的答案是只设置一个类 body 也许,然后让您的css规则完成其余的工作。你甚至可以通过这种方式添加过渡,这将允许你设置从白色到黑色等过渡的动画。我把过渡规则放在这里,你可以根据需要删除。

const body = document.querySelector('body');
window.onscroll = function() {
  if (window.pageYOffset > 100) body.classList.add('scrolled')
  else body.classList.remove('scrolled')
}

css:


# rc_logo {

  background-image: url('http://localhost/assets/img/nav-logo.png');
}

# centered_nav {

  background: transparent;
  box-shadow: none;
  transition: background .5s linear .5s;
}
.link1 {
  color: #fff;
  transition: background .5s linear .5s;
}

body.scrolled #rc_logo {
  background-image: url('http://localhost/assets/img/nav-logo-color.png');
}
body.scrolled #centered_nav {
  background: #000;
  box-shadow: 0px 2px 1px #E55200;
  transition: background .5s linear .5s;
}
body.scrolled .link1 {
  color: #000;
}

相关问题