简体   繁体   English

向下滚动时导航栏更改,如何使更改响应?

[英]Navbar change when scrolling down, how to make the change responsive?

I am creating a basic web page where I made a very basic transparent navbar with a white fontor font overlapping a dark background banner.我正在创建一个基本的 web 页面,我在其中制作了一个非常基本的透明导航栏,其中白色字体字体与深色背景横幅重叠。 The font turns black black when scrolling down and reaching a light background (at least that's the goal).向下滚动并到达浅色背景时,字体变为黑色(至少这是目标)。

The thing is, the change is not responsive, meaning that depending on the device and the size of the screen, the navbar change will not occur exactly at the bottom end of the banner.问题是,更改不是响应式的,这意味着根据设备和屏幕大小,导航栏的更改不会完全发生在横幅的底部。 Is there a way to do that?有没有办法做到这一点? Here is my code:这是我的代码:

1/ HTML 1/ HTML

home skills portfolio bio resume contact家庭技能组合生物简历联系方式

<div class="banner">
  <div class="background-gradient">
    <video autoplay muted loop id="myVideo">
      <source src="images/Background_Loop_small.mp4" type="video/mp4">
    </video>
  </div>

  <div class="content">
    <div class="presentation">
      <img src="images/avatar2.jpg" id="avatar">
      <h1 id="place-title">Olivier Girardot</h1>
      <h3 id="text-white">Junior Full-Stack Web Developper</h3>

      <section id="section01" class="demo">
        <div class="container" style="height: 80px"></div>
        <a href="#section02"><span></span><span></span><span></span></a>
      </section>
    </div>
  </div>
</div>

2/ CSS 2/ CSS

.navbar {
  margin: 0;
  background: transparent;
  position: fixed;
  color: white;
  display: flex;
  height: 60px;
  z-index: 10;
}

.navbar-links {
  display: flex;
  align-items: center;
  padding: 0px 10px;
}

.navbar-links a:hover {
  opacity: 0.7;
}

.menu {
  position: relative;
  display: flex;
  align-items: center;
  right: 0;
}

.menu a {
  color: white;
  text-decoration: none;
  font-size: 1vw;
  font-weight: 500;
  padding: 0px 10px;
}

.menu a:hover {
  border-bottom: solid white 1px;
}

.navbar-under {
    margin: 0;
    background: #f0f0f0;
    position: fixed;
    color: black;
    display: flex;
    height: 30px;
    z-index: 10;
}

.menu-under {
  position: relative;
  display: flex;
  align-items: center;
  right: 0;
}

.menu-under a {
  color: black;
  text-decoration: none;
  font-size: 1vw;
  font-weight: 500;
  padding: 0px 10px;
}

.menu-under a:hover {
  border-bottom: solid black 1px;
}

3/ Javascript 3/ Javascript

const navbar = document.querySelector(".navbar");
const menu = document.querySelector(".menu");

window.addEventListener('scroll', () => {
  if (window.scrollY > 700) {
    navbar.classList.remove('navbar');
    navbar.classList.add("navbar-under");
  }
});

window.addEventListener('scroll', () => {
  if (window.scrollY < 700) {
    navbar.classList.remove("navbar-under");
    navbar.classList.add('navbar');
  }
});

window.addEventListener('scroll', () => {
  if (window.scrollY > 700) {
    menu.classList.remove('menu');
    menu.classList.add("menu-under");
  }
});

window.addEventListener('scroll', () => {
  if (window.scrollY < 700) {
    menu.classList.remove("menu-under");
    menu.classList.add('menu');
  }
});

So I guess the issue comes from window.scrollY > 700 or window.scrollY < 700 , is there a way to make that responsive, and make the navbar change exactly at the bottom of the banner?所以我猜这个问题来自window.scrollY > 700window.scrollY < 700 ,有没有办法让它响应,并使导航栏完全在横幅底部发生变化?

Instead of listening to scroll event you should have a look at Intersection Observer (IO)您应该看看Intersection Observer (IO) ,而不是听滚动事件

With this you can react whenever an element overlaps with another element or the viewport, so you could use this for your navigation bar.有了这个,您可以在元素与另一个元素或视口重叠时做出反应,因此您可以将其用于导航栏。

Here is a quick example on how this might work.这是一个关于这可能如何工作的简单示例。 It's not perfect but it should provide a good starting point for you to flesh it out.它并不完美,但它应该为您提供一个很好的起点来充实它。

If you need to support older browsers like IE, have a look at the official polyfill from w3c如果您需要支持 IE 等旧版浏览器,请查看w3c 的官方 polyfill

 const sections = document.querySelectorAll('div'); const config = { rootMargin: '0px', threshold: [.2, .9] }; const observer = new IntersectionObserver(function (entries, self) { entries.forEach(entry => { if (entry.isIntersecting) { var headerEl = document.querySelector('header'); if (entry.intersectionRatio > 0.9) { //intersection ratio bigger than 90% //-> set header according to target headerEl.className=entry.target.dataset.header; } else { //-> check if element is coming from top or from bottom into view if (entry.target.getBoundingClientRect().top < 0 ) { headerEl.className=entry.target.dataset.header; } } } }); }, config); sections.forEach(section => { observer.observe(section); });
 * { margin: 0; padding: 0; box-sizing: border-box; }.g-100vh { height: 100vh } header { min-height: 50px; position: fixed; background-color: green; width: 100%; } header.white-menu { color: white; background-color: black; } header.black-menu { color: black; background-color: white; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <header> <p>Header Content </p> </header> <div class="grid-30-span g-100vh white-menu" style="background-color:darkblue;" data-header="white-menu"> </div> <div class="grid-30-span g-100vh black-menu" style="background-color:lightgrey;" data-header="black-menu"> </div> <div class="grid-30-span g-100vh white-menu" style="background-color:darkblue;" data-header="white-menu"> </div>

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

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