Certain characteristics remain constant while others experience variations

I have a code that successfully changes the header opacity when scrolling down, but now I want it to change the background color instead. Strangely, the code does not seem to affect this attribute, although it works fine for other attributes like opacity and transition duration. Why is it failing to change the background color?

When I tried uploading the code demo to this site, there was a display error even though it functions correctly on my website:

<script type="text/javascript>
var headerWrap = $('#header-wrap');
$(window).scroll(function() {
    headerWrap.addClass('scroll-opacity-change');
    if($(this).scrollTop() === 0) {
        headerWrap.removeClass('scroll-opacity-change');
    }
});
</script>
#header-wrap{
background:#D6ECFF;
width:100%;
height:auto;
border-bottom: 1px solid #CCC;
position:fixed;
top:0;/* may not be needed but no harm in having */
z-index:100000;
/* margin:0 auto; needed? */
}
.scroll-opacity-change{
opacity:0.7;
-webkit-transition-duration: 1.0s; /* Safari */
    transition-duration: 1.0s;
background:#777a7c;
}

Answer №1

The rules for applying css properties in your example are based on specificity.

For more information, you can visit:

Remember: An id takes precedence over a class, which in turn takes precedence over an element.

Because of this, the background property from the id class is being applied. To override it, you can either make the background property in the class !important (as Importance has precedence) or use #header-wrap.scroll-opacity-change.

var headerWrap = $('#header-wrap');
$(window).scroll(function() {
    headerWrap.addClass('scroll-opacity-change');
    if($(this).scrollTop() === 0) {
        headerWrap.removeClass('scroll-opacity-change');
    }
});
#header-wrap{
background:#D6ECFF;
width:100%;
height:100px;;
border-bottom: 1px solid #CCC;
position:fixed;
top:0;/* may not be needed but no harm in having */
z-index:100000;
/* margin:0 auto; needed? */
}
#header-wrap.scroll-opacity-change{
opacity:0.7;
    background:#000;
-webkit-transition-duration: 1.0s; /* Safari */
    transition-duration: 1.0s;

}

body
{
  height:1200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header-wrap">
</div>

Answer №2

Your styling has a specificity issue that needs to be addressed. The ID selector holds more weight than the class selector, so you must increase the specificity of the class selector by incorporating the ID into it.

Modify

.scroll-opacity-change{ 

to

#header-wrap.scroll-opacity-change {

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

Animating the variable's width with jQuery

Having difficulty achieving smooth animation on a variable set div width. It should animate similarly to a progress bar. A demo can be found here. Any suggestions on how to improve the animation for smoother transitions? Thanks in advance! Here is the HTM ...

What is the best way to design a multi-submit form without the need for page refreshing?

I am interested in creating a form with multiple questions where users can click "next" for each question without the page reloading. For instance Click on "Begin" On this page, there is no reload when choosing a radio option and clicking "next". How ca ...

Tips for maintaining the active tab after a page refresh by utilizing the hash in the URL

I came across this code snippet: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="680a07071c1b1c1c1a0918285d465a465b">[email protected]</a>/dist/css/bootstrap.min.css" ...

JsDoc presents the complete code instead of the commented block

I have a VUE.JS application that needs to be documented. For .VUE components, we are using Vuese. However, when it comes to regular JS files like modules, we decided to use JsDoc. I have installed JsDoc and everything seems fine, but when I generate the HT ...

What steps should I follow to create a large Angular single page application using ASP.NET MVC?

After gaining some experience with AngularJS on a small project, I am now ready to tackle a larger application. My plan is to create a single-page application using asp.net, making WebAPI calls and loading AngularJS views. However, I am unsure about how ...

A guide on implementing a jQuery click function on ng-click in an AngularJS application

Currently, I am developing a mobile app and utilizing MDL for the app's UI along with Angular JS. The theme I purchased from ThemeForest is called "FAB." My goal is to display data from the server using API's and showcase all the products that ar ...

Unusual situation observed in ExpressJS: Callback function fails to execute

Currently, I am facing an issue with my web app built using expressjs and node. It seems that the functionality is not working correctly. An unusual situation has occurred where accessing the first link in the browser yields the expected results, while th ...

Remove duplicate entries and store them in an array while also applying an extra condition in JavaScript

I'm looking to filter a list of objects based on different city and state values, then create a new list of cities from the filtered objects where the temperature is less than 40. The condition is that both the state and city should not be the same. l ...

Unable to access setRowData() in AgGrid/Angular 2 leads to rendering of the grid without displaying any rowData

Resolved I think my solution is temporarily fixed and it reveals that I may have set up my mongoose model incorrectly. The answer provided did assist me in solving my issue, but ultimately the reason for the empty data rows was due to incorrect identifier ...

What steps can I take to resolve the CLIENT_MISSING_INTENTS issue?

After diving into learning about discord.js, I've run into a bit of a roadblock. Despite trying to troubleshoot by searching online, I can't seem to resolve the issue. const Discord = require('discord.js'); // const Discord = require(&a ...

Unable to execute a JavaScript function when triggered from an HTML form

This is the code for a text conversion tool in HTML: <html> <head> <title> Text Conversion Tool </title> <script type="text/javascript"> function testResults(form) { var str = form.stringn.value; var strArray = str.split(" ...

Wind - Best practices for managing the status of multiple entities within a single display prior to executing the save changes function

In my system, there are three main entities: Project, Attachment, and Messages. A project can have multiple attachments and messages associated with it. The issue arises on the project detail view, where I display the project's messages and any attac ...

The TypeScript compiler is unable to locate the name 'window'

Within my Meteor/React project, I encounter the following line of code: let gameId = window.prompt("Please input the ID of the game you would like to load."); The TypeScript compiler presents an error during transpiling: Cannot find name 'window&apo ...

Troubleshooting why passing $var within @{{ }} in Vue.js + Laravel is not functioning as expected

I am integrating Algolia Vue-InstantSearch into a Laravel Project. Since we are working with a blade file, the {{ }} symbols will be recognized by the php template engine. To ensure that they are only understood by Vue in JavaScript, we can add an @ prefix ...

Attempting to utilize solution for the "ajax-tutorial-for-post-and-get" tutorial

I've been exploring the implementation of the second answer provided in a post about Ajax tutorials on a popular coding forum. Despite following the instructions, I encountered an issue where the $.ajax script, triggered by an "onclick" function, only ...

Avoiding Ajax overload/server collapse in an Arduino/ESP8266 setting

I've recently been delving into Arduino programming to host an HTML/CSS/JavaScript webpage on an Adafruit HUZZAH ESP8266 breakout board. Please pardon any unconventional methods I may be using. My approach involves utilizing Ajax to update pressure g ...

Having extra space can occur when adding margin to a div within another div in HTML5

I am facing an issue with the following piece of code: <body> <div class="page-top"> * </div> <div class="page-bottom"> <div class="contentbox"> <h1>CONTENTBOX</h1> </div ...

I am interested in utilizing a variable's value as an ID within a jQuery function

When working with jQuery, I often use a variable to store values. For example: var x = "ID1"; To utilize the value of this variable as an ID in my jQuery functions, I simply concatenate it as shown below: $('#' + ID1).val(); Thank you for you ...

Trigger a function upon the addition of a class to a div element | Execute AnimatedNumber()

Is there a way to trigger a function when a specific div receives a certain class? I have an animated div that only appears when scrolling, and once it's visible, it gains the class "in-view". I want to execute a function every time this div has the ...

Encircle a circle within the Progress Tracker

Is there a way to create a ring around the circle in this progress tracker, similar to the image shown here? The progress tracker is based on You can view an example here. The CSS approach used was: .progress-step.is-active .progress-marker { backgrou ...