简体   繁体   English

如何通过滚动使导航栏文本颜色发生变化

[英]How to make Navbar text colour change by scrolling

I need my sticky Navbar text to be white when it's on the top of my page, but when I scroll down few px I need the text to change to black (that's because I use transparent header background)当我的粘性导航栏文本位于页面顶部时,我需要它是白色的,但是当我向下滚动几个 px 时,我需要将文本更改为黑色(那是因为我使用透明的 header 背景)

I'm willing to create something like the navbar behavior at this site https://www.rolex.com我愿意在此站点https://www.rolex.com上创建类似导航栏行为的东西

I use WordPress Elementor.我使用 WordPress Elementor。 I've managed to make my navbar disappear when scrolling down and popup when scrolling up.我设法让我的导航栏在向下滚动时消失并在向上滚动时弹出。 and to be transparent on the top of the page and white background otherwise.并在页面顶部透明,否则为白色背景。

Since you are using WordPress - you might want to use jQuery for changing the color of your text in the navbar.由于您使用的是 WordPress - 您可能希望使用 jQuery 来更改导航栏中文本的颜色。

Here is the code example which makes your elements <a> and <p> turn black when you scroll down and pass 10px threshold and it's back to white when you are at the top of the page (accurately - less than 10px to the top, but you can change the breakpoint in the code to your needs).这是代码示例,当您向下滚动并通过 10px 阈值时,它会使您的元素<a><p>变黑,当您位于页面顶部时它又变回白色(准确地说 - 到顶部不到 10px,但您可以根据需要更改代码中的断点)。

With the given HTML code:使用给定的 HTML 代码:

<nav class="header">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Contact</a>
  <p>Company</p>
</nav>

You apply the following JavaScript code:您应用以下 JavaScript 代码:

const yourText = $('.header').find('a, p');
$(window).on('scroll', function () {
  const headerOffset = $('.header').offset().top;
  const breakPoint = 10;
  const defaultColor = 'black';
  if (headerOffset > breakPoint) {
    // you scroll down
    yourText.css('color', 'red');
  } else {
    // you are at the top of the page
    yourText.css('color', defaultColor);
  }
});

where you listen to "scroll" event and change the color based on the relative position of your header to the top of the page.您在其中收听“滚动”事件并根据 header 的相对 position 更改颜色到页面顶部。

PS If you use EcmaScript 5 version feel free to change const to var . PS 如果您使用 EcmaScript 5 版本,请随意将const更改为var But in general, you should be fine.但总的来说,你应该没问题。

If I understood right, you wanted something like this (I didn't use anything other than pure HTML, CSS and JS):如果我理解正确,你想要这样的东西(除了纯 HTML、CSS 和 JS 之外,我没有使用任何其他东西):

 window.onscroll = function() {scrollFunction()}; var navbar = document.getElementById("navbar"); function scrollFunction() { if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) { navbar.style.height = "5px"; navbar.style.backgroundColor = "black"; navbar.style.color = "white"; navbar.style.lineHeight = "5px"; } else { navbar.style.height = "20px"; navbar.style.backgroundColor = "gray"; navbar.style.color = "black"; navbar.style.lineHeight = "20px"; } }
 .navbar { width: 100%; height: 20px; padding: 10px; line-height: 20px; position: fixed; top: 0; color: black; background-color: gray; text-align: center; transition: all 0.3s ease; }.content { width: 100%; height: 900px; background-color: #c1c1c1; }
 <div id="navbar" class="navbar">I'm gonna change !</div> <div id="content" class="content"></div>

You mean something like this?你的意思是这样的?

Because you didn't add your code in the answer, I can't specifically your code, but maybe my code will help you to adapt your code with my example.因为您没有在答案中添加代码,所以我无法具体说明您的代码,但也许我的代码会帮助您使用我的示例调整代码。

The navbar color you can change if you want您可以根据需要更改的导航栏颜色

 $(function () { $(document).scroll(function () { var $nav = $(".navbar-fixed-top"); $nav.toggleClass('scrolled', $(this).scrollTop() > $nav.height()); }); });
 @import "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css"; body { padding-top: 70px; background: white; }.navbar-fixed-top.scrolled { background-color: white;important: color; black: transition; background-color 200ms linear. }.navbar-fixed-top.scrolled:nav-link { color; black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <header> <nav id="navigation" class="navbar navbar-dark bg-primary navbar-fixed-top"> <ul class="nav navbar-nav"> <li class="nav-item"><a href="#" class="nav-link">Site1</a></li> <li class="nav-item"><a href="#" class="nav-link">Site2</a></li> <li class="nav-item"><a href="#" class="nav-link">Site3</a></li> </ul> </nav> </header> <p> Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test </p>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM