Search code examples
javascripthtmlcssbootstrap-4

How to make gradient merge into navbar instead of "pop" the color changes?


I am trying to make a sticky navbar go into a gradient and then come back to black on it's fixed position, but when it moves it should smoothly merge into a gradient.

When I use the navbar to scroll down and turn from black to a gradient it works but when I go back up it pops into black instead of a smooth transition. how to go about this?

I want the gradient to have the same behavior as when I scroll down but when I scroll back up it dosen't do the same.

window.addEventListener("scroll", function () {
  const navbar = document.querySelector(".navbar");
  const scrollToTopBtn = document.getElementById("scrollToTopBtn");
  const img = scrollToTopBtn.querySelector("img");

  // Handle navbar background transition
  if (window.pageYOffset > 21) {
    navbar.classList.add("scrolled");
  } else {
    navbar.classList.remove("scrolled");
  }
});
body,
html {
  margin: 0;
  padding: 0;
  font-family: Arial, sans-serif;
  scroll-behavior: smooth;
  height: 2000px; /* For demonstration purposes */
}
.navbar {
  position: -webkit-sticky; /* For Safari */
  position: sticky;
  top: 21px; /* Sets the navbar 21px from the top */
  width: 100%;
  background-color: black; /* Start with a black background */
  transition: background 0.5s ease; /* Smooth transition for background */
  z-index: 1050;
}
.navbar.scrolled {
  background: radial-gradient(
    circle,
    rgba(0, 0, 0, 0.7) 70%,
    rgba(0, 0, 0, 0)
  ); /* Background gradient on scroll */
}
.navbar-dark .navbar-nav .nav-link {
  color: rgba(255, 255, 255, 0.55);
}
.navbar-dark .navbar-nav .nav-link:hover,
.navbar-dark .navbar-nav .nav-link:focus {
  color: rgba(255, 255, 255, 0.75);
}
.navbar-dark .navbar-nav .nav-link.active {
  color: #fff; /* Ensure this is white */
}
.navbar-nav .nav-link.active,
.navbar-nav .nav-link.show {
  color: turquoise;
}
.navbar-nav {
  flex-direction: row !important; /* Ensures navbar items are in a row */
}
.dropdown-menu {
  position: absolute !important; /* Ensures dropdown opens as an overlay */
}
.nav-link {
  color: white !important; /* Customize color as needed */
}
.navbar-nav {
  display: flex;
  flex-direction: row; /* Inline display */
  justify-content: center;
  align-items: center; /* This ensures vertical alignment is centered */
  width: 100%; /* Ensures the navbar takes full width */
}
.container-fluid {
  display: flex;
  flex-wrap: wrap; /* Allows items to wrap in smaller screens */
  align-items: center; /* Vertical alignment */
  justify-content: space-between; /* Horizontal spacing */
  width: 100vw;
}
@media (max-width: 768px) {
  .navbar-nav {
    flex-direction: column; /* Stack the links vertically on small screens */
  }
  .nav-link {
    padding: 0px 30px; /* Adjust padding for smaller screens */
    text-align: center; /* Center-align the text */
  }
}
.scroll-to-top {
  position: fixed;
  bottom: 30px;
  right: 30px;
  cursor: pointer;
  background-color: #fff;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  transition: background-color 0.3s;
}
.scroll-to-top:hover {
  background-color: #f8f8f8;
}
.scroll-to-top img {
  width: 40px;
  height: 40px;
  transition: transform 2s linear;
}
.scroll-to-top:hover img {
  transform: rotate(360deg);
}
#scrollToTopBtn {
  position: fixed;
  bottom: 20px;
  right: 30px;
  padding: 10px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  display: none; /* Hidden by default */
  z-index: 1000; /* Make sure it's above other content */
  outline: none;
}
#scrollToTopBtn img {
  width: 60px;
  height: 60px;
}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Navbar Scroll Effect</title>
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
  </head>
  <body>
    <nav class="navbar navbar-expand-lg navbar-dark">
      <div class="container-fluid">
        <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link active" href="#">Home</a>
          </li>
          <li class="nav-item dropdown">
            <a
              class="nav-link dropdown-toggle"
              href="#drawings"
              id="navbarDropdownMenuLink"
              role="button"
              data-bs-toggle="dropdown"
              aria-expanded="false">
              Drawings
            </a>
            <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <li><a class="dropdown-item" href="#">Drawing 1</a></li>
              <li><a class="dropdown-item" href="#">Drawing 2</a></li>
              <li><a class="dropdown-item" href="#">Drawing 3</a></li>
              <li><a class="dropdown-item" href="#">Drawing 4</a></li>
              <li><a class="dropdown-item" href="#">Drawing 5</a></li>
            </ul>
          </li>
        </ul>
      </div>
    </nav>
    <button id="scrollToTopBtn" onclick="window.scrollTo({top: 0, behavior: 'smooth'});">
      <img
        src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/Yin_yang.svg/1024px-Yin_yang.svg.png" alt="Go to top" />
    </button>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  </body>
</html>


Solution

  • You can use @property to animate gradient percent and color.

    But it's not supported by firefox.

    @property --p {
      syntax: "<percentage>";
      inherits: false;
      initial-value: 100%;
    }
    @property --c {
      syntax: "<color>";
      inherits: false;
      initial-value: black;
    }
    .navbar {
      --p: 100%;
      --c: black;
      background: radial-gradient(circle, var(--c) var(--p), transparent); /* Start with a black background */
      transition: --p 0.5s, --c 0.5s; /* Smooth transition for background */
    }
    .navbar.scrolled {
      --p: 70%;
      --c: rgba(0, 0, 0, 0.7);
    }
    

    window.addEventListener("scroll", function () {
      const navbar = document.querySelector(".navbar");
      const scrollToTopBtn = document.getElementById("scrollToTopBtn");
      const img = scrollToTopBtn.querySelector("img");
    
      // Handle navbar background transition
      if (window.pageYOffset > 21) {
        navbar.classList.add("scrolled");
      } else {
        navbar.classList.remove("scrolled");
      }
    });
    body,
    html {
      margin: 0;
      padding: 0;
      font-family: Arial, sans-serif;
      scroll-behavior: smooth;
      height: 2000px; /* For demonstration purposes */
    }
    @property --p {
      syntax: "<percentage>";
      inherits: false;
      initial-value: 100%;
    }
    @property --c {
      syntax: "<color>";
      inherits: false;
      initial-value: black;
    }
    .navbar {
      --p: 100%;
      --c: black;
      position: -webkit-sticky; /* For Safari */
      position: sticky;
      top: 21px; /* Sets the navbar 21px from the top */
      width: 100%;
      background: radial-gradient(circle, var(--c) var(--p), transparent); /* Start with a black background */
      transition: --p 0.5s, --c 0.5s; /* Smooth transition for background */
      z-index: 1050;
    }
    .navbar.scrolled {
      --p: 70%;
      --c: rgba(0, 0, 0, 0.7);
    }
    .navbar-dark .navbar-nav .nav-link {
      color: rgba(255, 255, 255, 0.55);
    }
    .navbar-dark .navbar-nav .nav-link:hover,
    .navbar-dark .navbar-nav .nav-link:focus {
      color: rgba(255, 255, 255, 0.75);
    }
    .navbar-dark .navbar-nav .nav-link.active {
      color: #fff; /* Ensure this is white */
    }
    .navbar-nav .nav-link.active,
    .navbar-nav .nav-link.show {
      color: turquoise;
    }
    .navbar-nav {
      flex-direction: row !important; /* Ensures navbar items are in a row */
    }
    .dropdown-menu {
      position: absolute !important; /* Ensures dropdown opens as an overlay */
    }
    .nav-link {
      color: white !important; /* Customize color as needed */
    }
    .navbar-nav {
      display: flex;
      flex-direction: row; /* Inline display */
      justify-content: center;
      align-items: center; /* This ensures vertical alignment is centered */
      width: 100%; /* Ensures the navbar takes full width */
    }
    .container-fluid {
      display: flex;
      flex-wrap: wrap; /* Allows items to wrap in smaller screens */
      align-items: center; /* Vertical alignment */
      justify-content: space-between; /* Horizontal spacing */
      width: 100vw;
    }
    @media (max-width: 768px) {
      .navbar-nav {
        flex-direction: column; /* Stack the links vertically on small screens */
      }
      .nav-link {
        padding: 0px 30px; /* Adjust padding for smaller screens */
        text-align: center; /* Center-align the text */
      }
    }
    .scroll-to-top {
      position: fixed;
      bottom: 30px;
      right: 30px;
      cursor: pointer;
      background-color: #fff;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
      transition: background-color 0.3s;
    }
    .scroll-to-top:hover {
      background-color: #f8f8f8;
    }
    .scroll-to-top img {
      width: 40px;
      height: 40px;
      transition: transform 2s linear;
    }
    .scroll-to-top:hover img {
      transform: rotate(360deg);
    }
    #scrollToTopBtn {
      position: fixed;
      bottom: 20px;
      right: 30px;
      padding: 10px;
      border: none;
      border-radius: 5px;
      cursor: pointer;
      display: none; /* Hidden by default */
      z-index: 1000; /* Make sure it's above other content */
      outline: none;
    }
    #scrollToTopBtn img {
      width: 60px;
      height: 60px;
    }
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Navbar Scroll Effect</title>
        <link
          rel="stylesheet"
          href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
      </head>
      <body>
        <nav class="navbar navbar-expand-lg navbar-dark">
          <div class="container-fluid">
            <ul class="navbar-nav">
              <li class="nav-item">
                <a class="nav-link active" href="#">Home</a>
              </li>
              <li class="nav-item dropdown">
                <a
                  class="nav-link dropdown-toggle"
                  href="#drawings"
                  id="navbarDropdownMenuLink"
                  role="button"
                  data-bs-toggle="dropdown"
                  aria-expanded="false">
                  Drawings
                </a>
                <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                  <li><a class="dropdown-item" href="#">Drawing 1</a></li>
                  <li><a class="dropdown-item" href="#">Drawing 2</a></li>
                  <li><a class="dropdown-item" href="#">Drawing 3</a></li>
                  <li><a class="dropdown-item" href="#">Drawing 4</a></li>
                  <li><a class="dropdown-item" href="#">Drawing 5</a></li>
                </ul>
              </li>
            </ul>
          </div>
        </nav>
        <button id="scrollToTopBtn" onclick="window.scrollTo({top: 0, behavior: 'smooth'});">
          <img
            src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/Yin_yang.svg/1024px-Yin_yang.svg.png" alt="Go to top" />
        </button>
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
      </body>
    </html>