I'm currently working on a website that features a sticky navbar, and I'm trying to implement a symbol that appears when the navbar reaches the very top of the page as the user scrolls down. However, I'm encountering an issue where the symbol does not reappear when the navbar leaves the top of the page as the user scrolls up.
Below you'll find the JavaScript code and HTML for the navigation:
$(document).ready(function() { var distance = $('#navbar').offset().top, $window = $(window); $(window).scroll(function() { if ($window.scrollTop() < distance) { $("#nav").remove( "<li id=\"navSymbol\"><a href=\"#\">▲</a></li>"); } if ($window.scrollTop() >= distance) { if ($("#navSymbol").length) { console.log("not adding") } else { $("#nav").prepend( "<li id=\"navSymbol\"><a href=\"#\">▲</a></li>"); } } }); });
<head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <div style="height: 25vw;"></div> <div class="sticky-top" id="navbar" style="padding-top: 3vw; padding-bottom: 3vw; background-color: white;"> <ul class="nav justify-content-center sticky-top" id="navcontent"> <li class="nav-item"> <a class="nav-link active" id="nav" href="#about">About</a> </li> <li class="nav-item"> <a class="nav-link" id="nav" href="#">Staffing</a> </li> <li class="nav-item"> <a class="nav-link" id="nav" href="#">Marketing and Finance</a> </li> <li class="nav-item"> <a class="nav-link" id="nav" href="#">Future Prospects</a> </li> </ul> </div> <div style="height: 100vw;"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"> </script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"> </script>
If anyone can offer some assistance, it would be greatly appreciated!