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.

<!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">
    <style>
        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;
        }
    </style>
</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>
        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');
            }
   
        });
    </script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật