滚动时无法更改导航栏的颜色

Posted

技术标签:

【中文标题】滚动时无法更改导航栏的颜色【英文标题】:Not able to change the color of navbar when I scroll 【发布时间】:2022-01-04 09:39:28 【问题描述】:

请帮助我是 css 新手

滚动时无法将导航栏颜色更改为红色

我认为问题出在我使用的 jquery 上,如果我错了,请纠正我

我正在使用引导程序 4

下面是我使用的 html

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    
    <!-- Google Font -->
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,300,400,700" rel="stylesheet">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    
    <!-- Custom CSS -->
    <link rel="stylesheet" href="app.css">

    <title>Museum of Candy</title>
  </head>
  
  <body>


    <nav id="MainNavbar" class="navbar navbar-expand-md navbar-dark py-0 fixed-top">
      <a href="#" class="navbar-brand">CANDY</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
        <div class="navbar-nav">
          <a class="nav-link" href="#">HOME</a>
          <a class="nav-link" href="#">ABOUT</a>
          <a class="nav-link" href="#">TICKETS</a>
        </div>
      </div>
    </nav>

    <div id="text" class="container-fluid px-0">
      <div class="row align-items-center">
        <div class="col-lg-6 text-white text-center d-none d-lg-block mt-5">
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
          <h1 class="">MUSEUM<span class="slash">/</span>OF<span class="slash">/</span>CANDY</h1>
        </div>
        <div class="col-lg-6">
          <img src="imgs/hand2.png"  class="img-fluid">
        </div>
      </div>
    </div>

    <section class="container-fluid px-0">
      <div class="row align-items-center content">
        <div class="col-md-6  order-md-1 order-2">
          <img src="imgs/milk.png"  class="img-fluid">
        </div>
        <!-- text-center here aligns every element inside the div to newline and the text inside it to middle that is the reason we use text center instead of "d-flex justify-content-center" class -->
        <div class="col-md-6 text-center order-md-2 order-1">
           <div class="row justify-content-center">
              <div class="col-10 col-lg-8 blurb mb-md-0 mb-5">
                <h2 class="">MUSEUM OF CANDY</h2>
                <img src="imgs/lolli_icon.png"  class="d-none d-lg-inline">
                <p class="lead">Lorem ipsum dolor, sit amet consectetur adipisicing elit. At enim nihil dolorum vitae magni est, temporibus deleniti vero, eligendi consequuntur quibusdam assumenda sapiente repellat.</p>
              </div>
            </div>
          </div>
      </div>



      <div class="row align-items-center content">
        <div class="col-md-6">
          <div class="row justify-content-center">
            <div class="col-10 col-lg-8 text-center blurb mb-md-0 mb-5">
              <h2 class="">MUSEUM OF CANDY</h2>
              <img src="imgs/lolli_icon.png"  class="d-none d-lg-inline">
              <p class="lead">Lorem ipsum dolor, sit amet consectetur adipisicing elit. At enim nihil dolorum vitae magni est, temporibus deleniti vero, eligendi consequuntur quibusdam assumenda sapiente repellat.</p>
            </div>
          </div>
        </div>
        <!-- text-center here aligns every element inside the div to newline and the text inside it to middle that is the reason we use text center instead of "d-flex justify-content-center" class -->
        <div class="col-md-6 text-center">
          <img src="imgs/gumball.png"  class="img-fluid">
        </div>
      </div>




      <div class="row align-items-center content">
        <div class="col-md-6  order-md-1 order-2">
          <img src="imgs/sprinkles.png"  class="img-fluid">
        </div>
        <!-- text-center here aligns every element inside the div to newline and the text inside it to middle that is the reason we use text center instead of "d-flex justify-content-center" class -->
        <div class="col-md-6 text-center order-md-2 order-1">
           <div class="row justify-content-center">
              <div class="col-10 col-lg-8 blurb mb-md-0 mb-5">
                <h2 class="">MUSEUM OF CANDY</h2>
                <img src="imgs/lolli_icon.png"  class="d-none d-lg-inline">
                <p class="lead">Lorem ipsum dolor, sit amet consectetur adipisicing elit. At enim nihil dolorum vitae magni est, temporibus deleniti vero, eligendi consequuntur quibusdam assumenda sapiente repellat.</p>
              </div>
            </div>
          </div>
      </div>

      
    </section>


      

    <!-- Optional javascript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
   
    <script>
      $(function () 
          $(document).scroll(function () 
              var $nav = $("#mainNavbar");
              $nav.toggleClass("scrolled", $(this).scrollTop() > $nav.height());
          );
      );
  </script>
  
  </body>
</html>

我使用了一些我自己的 css

而我的css如下:

body
    background-color: #f5d9d5;
    font-family: "Nunito";


#MainNavbar
    font-size: 1.5rem;
    font-weight: 100;

#MainNavbar .navbar-brand
    color: #ea1c2c;
    font-size: 1.5rem;


#MainNavbar .nav-link
    color: white;


#MainNavbar .nav-link:hover
    color: #ea1c2c;


#text .slash
    color: #ea1c2c;


h1
    font-weight: 100;
    font-size: 4rem;
    


.blurb h2
    color: #ea1c2c;
    font-weight: 100;
    font-size: 2.5rem;


.blurb p
    font-weight: 100;
    font-size: 1.125rem;
    line-height: 2;
    color: #f498b8;


.content
    margin-top: 100px;
    margin-bottom: 100px;


.navbar.scrolled 
    background-color: red




@media (max-width:1200px)
    h1
        font-weight: 100;
        font-size: 3rem;
    
    .blurb h2
        font-size:2rem;
    

点击下面的链接查看我的项目文件夹结构

here is my project folder structure

【问题讨论】:

您的选择器引用似乎不正确,应该是 #MainNavbar 而不是 #mainNavbar。如果这仍然不起作用,请尝试确定问题是您的样式还是在指定条件下应用类的脚本 - 您可以通过在滚动事件侦听器函数中记录 (console.log($nav.hasClass('scrolled'))) 来做到这一点。每次满足条件时都应该注销。 非常感谢它已经解决了我从昨天开始再次遇到这个问题感谢您抽出时间帮助我???? 【参考方案1】:

您将需要 JavaScript 来执行此操作,请查看 this pen。

// Logic
header 
    width: 100%;
  min-height: 3em;
  background-color: #eee;


html:not([data-scroll='0']) 
    
    body 
      padding-top: 3em;
    
    
    header 
        position: fixed;
        top: 0;
        background-color: red;
        box-shadow: 0 0 .5em rgba(0, 0, 0, .5);
    
    



// Cosmetics
header 
    box-sizing: border-box;
    padding: 1em;
    transition: background-color .25s;


main 
    padding:0 1em;

Js

> // The debounce function receives our function as a parameter const
> debounce = (fn) => 
> 
>   // This holds the requestAnimationFrame reference, so we can cancel
> it if we wish   let frame;
> 
>   // The debounce function returns a new function that can receive a
> variable number of arguments   return (...params) => 
>     
>     // If the frame variable has been defined, clear it now, and queue for next frame
>     if (frame)  
>       cancelAnimationFrame(frame);
>     
> 
>     // Queue our function call for the next frame
>     frame = requestAnimationFrame(() => 
>       
>       // Call our function and pass any params we received
>       fn(...params);
>     );
> 
>     ;
> 
> 
> // Reads out the scroll position and stores it in the data attribute
> // so we can use it in our stylesheets const storeScroll = () =>   
> document.documentElement.dataset.scroll = window.scrollY; 
> 
> // Listen for new scroll events, here we debounce our `storeScroll`
> function document.addEventListener('scroll', debounce(storeScroll), 
> passive: true );
> 
> // Update scroll position for first time storeScroll();

【讨论】:

谢谢 armann shandilya 刚刚更新了代码 感谢您的参考

以上是关于滚动时无法更改导航栏的颜色的主要内容,如果未能解决你的问题,请参考以下文章

我不知道我在做啥错,我想在页面滚动时更改导航栏的背景颜色

iOS 7隐藏导航栏时如何更改状态栏的颜色?

在 Bootstrap 上更改导航栏的颜色 [重复]

在flutter中进入暗模式时更改系统导航和状态栏的颜色

如何更改特定视图上导航栏的颜色

如何更改导航栏的默认背景颜色? [复制]