降低网站上的导航栏高度

时间:2019-03-31 05:49:56

标签: jquery html css

我有一个用引导程序4制成的固定导航栏。我想在向下滚动页面时降低导航栏的高度。当我尝试向下滚动页面时。高度不会降低。下面是我的浏览器的视频链接

Link to the video i have tried

Want effect like this video

我已经尝试过的某些代码

$(document).ready(function() {
  $('.nav-button').click(function() {
    $('.nav-button').toggleClass('change');
  });

  $(window).scroll(function() {
    var position = $(this).scrollTop();
    if (position >= 200) {
      $('.nav-menu').addClass('.custom-navbar');
    } else {
      $('.nav-menu').removeClass('.custom-navbar');
    }
  });
});
body {
  font-family: 'Montserrat', sans-serif;
}


/* Header */

header {
  height: 100vh;
  background: linear-gradient(rgba(0, 0, 0, .4), rgba(0, 0, 0, .5)), url(images/header-img.jpeg) no-repeat center center/cover;
}


/* Navbar */

.nav-menu {
  background: linear-gradient(rgba(0, 0, 0, .3), rgba(0, 0, 0, .5));
  padding: 30px;
  transition: all .7s;
}

.menu-item {
  font-size: 13px;
  letter-spacing: 1px;
  color: #eee;
  transition: color .5s;
}

.menu-item:hover {
  color: #70aed2;
}

.line1,
.line2,
.line3 {
  width: 23px;
  height: 3px;
  margin: 5px;
  transition: all .4s;
}

.change .line1 {
  transform: rotate(-45deg) translate(-5px, 6px);
}

.change .line2 {
  opacity: 0;
}

.change .line3 {
  transform: rotate(45deg) translate(-5px, -6px);
}

.custom-navbar {
  padding: 5px 30px;
  background: linear-gradient(rgba(0, 0, 0, .6), rgba(0, 0, 0, .7));
}


/* End of Navbar */


/* Banner */

.banner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
}

.banner-heading {
  animation: anim 2s;
}

.banner-para {
  animation: anim 2s .5s backwards;
}

@keyframes anim {
  0% {
    transform: translateX(-100px);
    opacity: 0;
  }
  100% {
    transform: translateX(0);
    opacity: 1;
  }
}


/* End of Banner */


/* End of Header */
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Header -->
<header>
  <!-- Navbar -->
  <nav class="navbar navbar-expand-lg fixed-top nav-menu">
    <a href="#" class="navbar-brand text-light text-uppercase"><span class="h2 font-weight-bold">photo</span><span class="h1">X</span></a>
    <button class="navbar-toggler nav-button" type="button" data-toggle="collapse" data-target="#myNavbar">
            <div class="bg-light line1"></div>
            <div class="bg-light line2"></div>
            <div class="bg-light line3"></div>
          </button>
    <div class="collapse navbar-collapse justify-content-end text-uppercase font-weight-bold" id="myNavbar">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a href="#" class="nav-link m-2 menu-item">Home</a>
        </li>
        <li class="nav-item">
          <a href="#" class="nav-link m-2 menu-item">Mission</a>
        </li>
        <li class="nav-item">
          <a href="#" class="nav-link m-2 menu-item">Collection</a>
        </li>
        <li class="nav-item">
          <a href="#" class="nav-link m-2 menu-item">Gallery</a>
        </li>
        <li class="nav-item">
          <a href="#" class="nav-link m-2 menu-item">Customers</a>
        </li>
        <li class="nav-item">
          <a href="#" class="nav-link m-2 menu-item">Pricing</a>
        </li>
        <li class="nav-item">
          <a href="#" class="nav-link m-2 menu-item">Contact</a>
        </li>
      </ul>
    </div>
  </nav>
  <!-- End of Navbar -->

  <!-- Banner -->
  <div class="text-light text-md-right text-center banner">
    <h1 class="display-4 banner-heading">Welcome to <span class="text-uppercase">photo</span> <span class="display-3">X</span></h1>
    <p class="lead banner-para">Lorem ipsum dolor sit amet consectetur adipisicing.</p>
  </div>
  <!-- End of Banner -->
</header>
<!-- End of Header -->

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

1 个答案:

答案 0 :(得分:5)

当您要使用addClass和removeClass时,不应在类名之前添加点,这将起作用:

if(position >= 200) {
  $('.nav-menu').addClass('custom-navbar');
} else {
  $('.nav-menu').removeClass('custom-navbar');
}

检查此codepen,我认为这几乎是您要寻找的内容:

https://codepen.io/Masoudm/pen/mgdPVP