JavaScript code not running as expected, only the initial command is being executed

This question has been boggling my mind lately. I have a JavaScript function that used to work perfectly until I switched to the MVC model. The function is meant to reverse the colors of the navbar on scroll, changing it from white with grey text to grey with white text.

Below is the code for the function:

window.addEventListener("scroll", function () {
  const mainNav = document.getElementById("navbar");

  if (this.window.pageYOffset > 0) {
    mainNav.classList.add("navscroll");
    mainNav.classList.add("navscroll a");
    mainNav.classList.add("navscroll #navcustom_1:hover");
    mainNav.classList.add("navscroll #navcustom_2:hover");
    mainNav.classList.add("navscroll #navcustom_3:hover");
  } else {
    mainNav.classList.remove("navscroll");
    mainNav.classList.remove("navscroll a");
    mainNav.classList.remove("navscroll #navcustom_1:hover");
    mainNav.classList.remove("navscroll #navcustom_2:hover");
    mainNav.classList.remove("navscroll #navcustom_3:hover");
  }
});

Before scrolling:

https://i.sstatic.net/TOiw8.png

After scrolling, only the first line is not working:

https://i.sstatic.net/HdZ5e.png

The CSS code related to this issue:

.navscroll {
  background-color: #3f3f3f !important;
}

.navscroll a {
  color: white !important;
}

.navscroll #navcustom_1:hover {
  color: #b82be2 !important;
}

.navscroll #navcustom_2:hover {
  color: #e25822 !important;
}

.navscroll #navcustom_3:hover {
  color: #2e8b57 !important;
}

I'm also including the necessary files in header.php, required for all other pages:

<link rel="stylesheet" href="../CSS/style.css" />
<script src="../JS/functions.js"></script>

Here's the HTML structure of the navbar:

<nav class="navbar navbar-expand-md navbar-light bg-white sticky-top" id="navbar">
        <div class="container-fluid" id="containerF">
            <a class="navbar-brand" href="index.html">
                <h3>WildCampers</h3>
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarResponsive">
                <ul class="navbar-nav ml-auto" id="navbarAuto">

I'm really struggling to figure out why this issue is happening and it's driving me crazy. If you need more details, please let me know.

Answer №1

It seems like there may be a conflicting style in your CSS code. This could be due to the order of styles or the inclusion of another CSS file that is more specific. It's often better to increase the specificity of a style rather than simply using !important, as two conflicting styles can both have this rule.

#navbar.navscroll {
  background-color: #3f3f3f !important;
}

I believe adding an ID to the style might increase its specificity enough for it to take effect.

ALSO, you should consider removing the following lines of code as they could cause unwanted behavior:

mainNav.classList.add("navscroll a");
mainNav.classList.add("navscroll #navcustom_1:hover");
mainNav.classList.add("navscroll #navcustom_2:hover");
mainNav.classList.add("navscroll #navcustom_3:hover");

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

Retrieve the jQuery widget instance by selecting an element within the widget

I am currently developing a widget using the Jquery widget factory. The abbreviated version of the code looks something like this: _create: function(){ this.element.hide(); //hides original input element this.newInput=$("<input>"); //creates ...

No matter the way I input the URL in the AJAX call, the response always comes back as successful

I ran into an issue with my ajax request. It was supposed to be a GET request that returned some data, but no matter how I configured the URL, it always gave a success response even when it didn't actually succeed. var id = 5; $.ajax({ type: ...

Guidelines for capturing a webpage screenshot and sending it back to the client using asp.net and phantomjs

My goal is to read a screenshot of a website using phantomjs and ASP.NET. Initially, I attempted to save the screenshot to a file using page.render. This method worked smoothly in a console application but faced issues when called from an ASP.NET handler, ...

The JavaScript Discord Bot is having trouble connecting to a voice channel

I'm currently working on developing a discord bot using node.js. While I've successfully set it up to respond, I'm facing an issue with summoning it to a voice channel. Here is the snippet of code I am working with: switch (args[0]) { c ...

Getting the full referrer URL can be achieved by using various methods depending

I am currently working with ASP.Net. My goal is to retrieve the complete referrer URL when visitors arrive at my website from: https://www.google.co.il/webhp?sourceid=chrome-instant&rlz=1C1CHEU_iwIL457IL457&ion=1&espv=2&ie=UTF-8#q=%D7%90 ...

Switching videos upon clicking buttons

Hey there! I've got an interesting challenge for you. I have four mp4 videos featuring a cartoon character performing various emotes. My goal is to create a toggle switch that, when activated, will display the first video (intro) along with three butt ...

Cypress eliminating the "X-CSRFToken" header

There seems to be an issue with the Cypress test runner where it is removing X-CSRFToken from the request header, leading to a 403 Forbidden error. I have compared the headers between a manual run and a Cypress test run, and you can see the difference in t ...

SVGs do not display on iPhones

Having some issues with my code - specifically, I have an SVG animation that seems to work on all devices except iPhones. I've been struggling to find a solution. Here is the code snippet for the SVG: <div class="contenuto-svg"> <svg vi ...

Tips for sending data from a child component to a parent component

I am facing an issue with my components setup. I have 3 components - 2 child components and 1 parent component. The parent component has a save button, and in order to get data from the child components in the parent component, I have implemented the follo ...

``There seems to be a problem with the margins within a flex container

Can someone assist with fixing a bug related to the .half-img2 element's margin-top property? It is necessary for the heights of these two images to be equal to that of the main image. Here are the links for reference: http://prntscr.com/9 ...

Switching from hover to onload事件 in the JavaScript code

I am currently modifying some preexisting code to suit my requirements. https://tympanus.net/codrops/2010/02/08/awesome-css3-jquery-slide-out-button/ The sliding effect of the content when hovered with the mouse is what I am looking for. It seems straigh ...

What is the best way to eliminate the space following the image?

Something strange happened, and now there's a big gap following the top image. I was working on making the code responsive and then inserted this image, which messed everything up. Any ideas on what I might be doing wrong? I want the image to be close ...

Prevent selection based on function in Angular

I'm attempting to prevent certain options from being selected based on a specific method. For instance, let's say I have four options: A B C D In my method (let's use "x" as an example): if(name == A) { disable the selection for option A. ...

Show elements on a webpage using HTML and JavaScript

How can I showcase film1, film2, and film3 in my HTML code? I attempted: peliHtml.innerHTML = `${this.Film.title}; However, I keep encountering a console error. How should I go about displaying this data in the html? HTML <p id=" ...

Guidelines for populating data with an array

I have the following array: const dataArr = [[15, 14, 5], [16, 10, 2], [17, 6, 13], [18, 4, 8], [19, 7, 4]]; Now, I am populating another array using the above data as shown below: for (let i=0; i<dataArr.length; i++){ newData.push(dataArr[i]); ...

What is the best location to manage errors within a sequelize ORM query?

I am working with the Sequelize ORM within a Node/Express environment. My database has two tables: one for Users and another for Items. The Item table includes a foreign key that is linked to the UserId in the User table. Whenever I attempt to create an ...

How can I convert HTML to PDF with Angular 5?

I have 2 questions regarding my project built in ANGULAR 5. Firstly, I am seeking a method to convert HTML into PDF format without it being rendered as an image. The resulting PDF should be interactive with selectable text by the user. Additionally, plea ...

Preventing the occurrence of numerous ajax requests or halting ongoing requests

Currently, I am utilizing the flotchart plugin to track user interactions on a chart and retrieve additional information from a database based on the chart value. For instance, if a user hovers over a specific date like "24 May," an AJAX call is triggered ...

Incorporating City Capital Icons onto a Leaflet Map with JavaScript Integration

I've set up a layer on my leaflet map and have a capitalCities.js file that contains data for markers I want to add to the map. Here's a snippet from the capitalCities file: let capitals = [{"CountryName":"Somaliland","Ca ...

Modify the scoped variable using Angular's $resource module

I am facing an issue with my Angular application where I cannot get the results of a second $resource call to update a scoped variable. Initially, I am able to generate the initial view from an Angular $resource call to an external API and display the data ...