Trouble with addClass failing to update CSS properties

I'm having trouble updating my add class. I'm attempting to create a sticky navigation bar, but the CSS on my site doesn't seem to be updating. None of the solutions I've found so far have been helpful. The website in question is antetech.org

HTML:

<nav id='main-nav'>
         <a href="/index.php"><img class='logo' src='CSS/IMG/logo.png' alt='Logo'></a>

                <ul>
                   <li><a href='#home'> <button class="bttn-stretch bttn-md bttn-primary">Home</button> </a> </li>
                   <li><a href='#about'><button class="bttn-stretch bttn-md bttn-primary"> About </button></a></li>
                   <li><a href='#downloads'> <button class="bttn-stretch bttn-md bttn-primary">Software</button> </a> </li>
                   <li><a href='/support.php'><button class="bttn-stretch bttn-md bttn-primary"> Tech Support </button></a></li>
                </ul>
        </nav>    

CSS:

nav .onscroll {
    background: rgba(173, 9, 26, 0.5);
}

JQuery/JavaScript:

<script type="text/javascript">
     $(window).on('scroll', function(){
         if($(window).scrollTop()){
             $('nav').addClass('onscroll');
             $('nav').removeClass('default');
         }
         else 
             {
              $('nav').removeClass('onscroll');
              $('nav').addClass('default');
             }
     })
    </script>

In Chrome, the class appears to be updating, but no changes are being reflected. This issue extends beyond just the background color and applies to any modifications.

Answer №1

Your script written in Javascript is functioning properly and as anticipated; the culprit lies within your CSS.

To rectify the issue, you must adjust the selector nav .onscroll in style.css to nav.onscroll - essentially eliminating the space between "nav" and the class name. The reason behind this adjustment is that you are applying the class specifically to the nav element; by adding a space, it implies that .onscroll is a child element of nav.

Therefore, your CSS code should appear as follows:

nav.onscroll{
    background: rgba(173, 9, 26, 0.5);
}

Answer №2

To modify your CSS rule, simply eliminate the "nav" selector:

.onscroll{ background: rgba(173, 9, 26, 0.5); }    

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to dynamically change the title of a tab when new content is added to a minimized page?

Is anyone familiar with how websites like Facebook and Buzzfeed update their tab titles when there are multiple tabs open? I have noticed that sometimes they add a "(1)" or "(2)" to indicate changes on the page. Do they use JavaScript to achieve this eff ...

Minimize Postback Flashing on ASP.NET Website/Google Chrome Browser

When it comes to IE, these meta tags can help reduce the postback flicker: <meta http-equiv="Page-Enter" content="blendTrans(Duration=0)"> <meta http-equiv="Page-Exit" content="blendTrans(Duration=0)"> Is there a way to achieve a similar resu ...

Making an asynchronous call from Index.html to PHP Script

I am currently working on implementing an AJAX call to my PHP Script. While I can successfully echo the results from my data.php file, I am now facing a challenge regarding how to initiate the call from index.html in order to retrieve the table results s ...

Updating the content of a window without the need to refresh the page using JavaScript

Is there a way to navigate back to the previous window in chat_user without refreshing the entire page when the back button is clicked? Below is the code I have tried: <a href="" onclick="window.history.go(-1); return false;">back</a> ...

Tips on submitting JSON data to a server

I need the data to be structured like this {"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f0c3f18121e1613511c1012">[email protected]</a>","password":"1"} but it is currently appearing like this { & ...

Set the page to be rigid instead of flexible

I'm struggling to lock this webpage in place, so it doesn't resize when the browser window changes. The text keeps collapsing whenever I adjust the size of the browser, and I can't figure out what's going wrong. Can someone please help ...

Tips for staying on the same page when hovering over a table

My hover table contains multiple pages, with each row offering various actions one can take with an item. After performing an action, the page refreshes and returns to the first page of the table. Is there a way to stay on the same page after executing a ...

Creating a string specifically for implementing regular expressions in JavaScript

I'm currently working on a custom jQuery plugin known as jQuery-Faker. This plugin is designed to produce fake data for populating form fields based on their respective field names. One of the tasks I have set out to accomplish is generating phone num ...

Displaying information on a chartJS by connecting to an API using axios in VueJS

Struggling with inputting data into a ChartJS line-chart instance. The chart only displays one point with the right data but an incorrect label (named 'label'): Check out the plot image The odd thing is, the extracted arrays appear to be accura ...

The significance of Token Details in Tokbox

I'm currently working on developing a video chat platform that caters to various user roles - some may just observe while others actively participate in calls. I've been exploring the capabilities of the Tokbox Api () which allows metadata to be ...

Discovering the actual file path of an uploaded document in Asp.Net Mvc

When trying to use an HTML input file to select a file, I encountered an issue where JQUERY would return a fake path instead of the actual one. $("#inputfile").val() I am looking to obtain the selected file's actual path, such as: D:\Documen ...

Even when autocomplete is disabled, Firefox continue to cache hidden input fields

I'm encountering an issue with a form that has the autocomplete attribute set to off. Within this form, there is a hidden input element (created using ASP.NET MVC's Html.HiddenFor() method): <input type="hidden" value="0" name="Status" id="S ...

Tips for conducting end-to-end testing using Nest.js and MikroORM

Recently, I've been working on a Nest.js API app that utilizes SWC and Vitest. Below are the minimized files related to the Users module. CRUD users service: // users.service.ts ... @Injectable() @UseInterceptors(ClassSerializerInterceptor) export c ...

The headline box is displaying CSS code instead of the content. How can this issue be resolved?

Having an issue with a WordPress template that features a blue "headline" box that I need to change the color of to #333399. I tried inspecting the element, browsing through the code, and identified the code below as what needed modification: #headline, # ...

Is it just me or does escape() not work reliably?

With the use of JavaScript, I am able to generate HTML code dynamically. For instance, I can add a function that triggers when a link is clicked, like so: $('#myDiv').append('<a href="javascript:start(\''+TERM+'\ ...

The wonders of jQuery popup windows

A code was discovered that displays a popup, but it currently relies on transparency (opacity: 0). I would like to modify it to use display: none instead, as the transparent window in the center of my website is causing issues. Here is the JavaScript code ...

Can the THREE.OBJLoader create a shadow effect?

Trying to create a shadow for an object, There is a THREE.SpotLight as the light source in the scene Below is the code snippet for MTLLoader() and OBJLoader() var mtlLoader = new THREE.MTLLoader(); mtlLoader.setBaseUrl('assets/&apos ...

Ways to create a horizontal navigation menu using jQuery UI

Hey there, I'm really enjoying all the jQuery UI features! I have a question about the navigation menu - for some reason, I can't seem to get it to display horizontally. I've been playing around with the CSS but I must be missing something ...

Unfortunately, the YouTube iframe Embed feature does not have the ability to adjust audio settings on an

I've been experimenting with the YouTube iframe API and created a simple set of player controls for the YouTube player using Flash CC HTML5 canvas. The controls include a play/pause button, a draggable video bar with buffering visualization, and a vol ...

Alter the color of Material UI Raised Button when it is hovered over

I am trying to change the background color of a raised button on hover in Material UI. I attempted the following, but it did not work. Is there a way to modify the background color in Material UI for a raised button? Example.JS <RaisedButton ...