Encountering an issue with adding a background color to the nav bar upon scrolling, where it works randomly. Approximately 50% of the time, it functions correctly, and the remaining 50% results in a ReferenceError. This problem persists across various devices, browsers, and pages.
<script async src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(document).scroll(function () {
var $nav = $(".masthead");
$nav.toggleClass('navBG', $(this).scrollTop() > $nav.height());
});
});
</script>
.masthead{width:100%;height:80px;position:fixed;top:0;left:0;z-index:9999;box-shadow: 0 50px 50px -20px rgba(0, 0, 0, 0.2) inset;}
.masthead.navBG{box-shadow:0 5px 30px 5px rgba(0, 0, 0, 0.4);background-color:#01417E; !important;transition:background-color 100ms linear;}
<div class="masthead">
<nav id="sidenavContainer" class="sidenav">
<!-- SOME LIs -->
<!-- JS for Nav Functionality -->
</nav>
<div id="navTri" onclick="openNav()"><img src="menu.svg" alt="Menu" width="30"></div>
<div id="logoMain"><a href="#"><img src="logo.svg" alt="Logo"></a></div>
</div>
The error consistently appears in the provided code snippet. Any suggestions on how to resolve this?