After searching extensively on Stack Overflow and other websites, I have not been able to find a clear answer to my question regarding changing a CSS class (#navbar a) after a specific function is triggered. My understanding of jQuery and JavaScript is limited, so any help would be appreciated.
Here is the code snippet that I tried but did not work as intended:
$('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');
This is the JavaScript / jQuery code in question:
<script>
window.onscroll = function() {myFunction()};
var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
navbar.style.align = 'auto';
navbar.style.width = '100%';
$('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');
} else {
navbar.classList.remove("sticky");
navbar.style.width = '80%';
}
}
</script>
This is the content of the CSS file:
#navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
width: 25%;
text-decoration: none;
font-size: 35px;
padding: 20px 0px;
}
Below is the complete HTML code for reference:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width;initial-scale=1;height=device">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="Style.css">
</head>
<body style="background:#101010">
<div class="header">
<h2>FELLE</h2>
</div>
<div id="navbar">
<a class="active" href="">Hjem</a>
<a href="Historie.html">Historie</a>
<a href="Hyttefelt.html">Hyttefelt</a>
<a href="Annet.html">Annet</a>
</div>
<div class="container" align="middle">
<img src="Bilder/Felle_Butikk.jpg" alt="Felle butikk"/>
<object data="Tekster/Felle_Butikk.txt"></object>
<img src="Bilder/Solhomfjell.jpg" alt="Solhomfjell i nissedal"/>
<object data="Tekster/Solhomfjell.txt"></object>
</div>
<script>
window.onscroll = function() {myFunction()};
var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky")
navbar.style.align = 'auto';
navbar.style.width = '100%';
$('link[href="Style.css"]').addClass('#navbar a').css('width', '20px');
} else {
navbar.classList.remove("sticky");
navbar.style.width = '80%';
}
}
</script>
</body>
</html>