我正在尝试使粘性导航栏进入渐变状态,然后在其固定位置返回黑色,但是当它移动时,它应该平滑地合并到渐变中。
当我使用导航栏向下滚动并从黑色变为渐变时,它可以工作,但当我返回时,它会弹出为黑色,而不是平滑过渡。怎么办?
我希望渐变具有与向下滚动时相同的行为,但当我向上滚动时,它不会执行相同的操作。
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>
您可以使用 @property 来设置渐变百分比和颜色的动画。
但是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>