What are the steps to changing the navbar's color when scrolling down?

I am having trouble changing the color of my navigation bar from transparent to black after the user scrolls down. Despite trying various methods and watching tutorials, I can't seem to get it to work.

HTML

<nav role='navigation' class="sticky-top bg-transparent">
      <img class="navbar-brand" src="../static/website_Images/logo.jpg" alt="Logo" style="height:90px;">
      <div id="menuToggle">
        <input type="checkbox" />
            <span></span>
            <span></span>
            <span></span>
          <ul id="menu">
            <a href="/"><li>Home</li></a>
            <a href="/image_submit"><li>Submit Image</li></a>
            <a href="/image_search"><li>Search Image</li></a>
            <a href="/login"><li>Login/Sign up</li></a>
          </ul>
      </div>
    </nav>

JS

<script >
    $(window).scroll(function(){
     $('nav').toggleClass('scrolled', $(this).scrollTop() > 100);
     });
    </script>

CSS

.bg-transparent{
  height: 90px;
  }

.bg-transparent.scrolled{
  background: black !important;
}

Answer №1

It seems like there is an issue with your JavaScript code.
Try testing it using the following code snippet :

  window.onscroll = function () { scrollHeaderHandler() };

  function scrollHeaderHandler() {
    if (window.pageYOffset > 100) {
      $('nav').addClass('scrolled');
    } else {
      $('nav').removeClass('scrolled');
    }
  }

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

What is the best way to distribute multiple inline-block elements evenly?

Is it feasible to evenly distribute numerous elements in a div with adjustable width? Check out this example that doesn't work. While using text-align:center; will center the elements, margin:0 auto; does not have any effect. I am aiming for somethin ...

What can I do to prevent masonry images from piling on top of one another?

I have been working on creating a photo gallery using the Bootstrap 5 example on masonry, and everything is working well so far. I have also successfully implemented modal boxes. However, the only issue I'm facing is with the JS Image Loaded not preve ...

What is the best way to set a checkbox to null instead of false using Angular?

I am currently developing a filtering system that allows users to select different parameters to filter through a list of items. For instance, the item list could be a collection of dishes with filters represented as checkboxes labeled as vegan, vegetaria ...

Anomalous behavior of buttons in react-redux

Currently, I have a basic counter set up in react-redux as part of my learning process with these frameworks. My goal is to create a pair of number input fields that determine the payload for an increment/decrement action sequence. The intended outcome is ...

What is the process to set up a WebSocket on Tornado for a production server rather than on a local machine?

Recently, I've delved into the world of WebSockets but have only experimented with them in a localhost environment while developing locally. Following this informative tutorial on real-time data visualization: https://medium.com/@benjaminmbrown/real-t ...

Understanding how to display a component within another component in Vue.js

I am faced with a scenario where I have a component that has the following template: <div v-for:"item in store" v-bind:key="item.type"> <a>{{item.type}}</a> </div> Additionally, I have another component named 'StoreCompone ...

What is the best way to smoothly insert an item into a nested object within the state of a React component

Trying to create a complex array using react hook: const [strategy, setStrategy] = useState([leg]); Here's how the `leg` object is defined: const leg = { entry: { conditions: [], actions: [""], }, exit: { conditions: [], ...

How can I retrieve information from a topic using kafka-node?

Having trouble reading data from a Kafka server? You may encounter an error message stating that the topic does not exist. Here are some questions to guide you: 1- How can I ensure that my Kafka connection is established? 2- What is the process for retri ...

Update the display immediately upon a change in the state

In my app.js file, the code looks like this: export const App = () => { const [selectedMeals, setSelectedMeals] = useState<string[]>(["allItems"]); const onCheckHandler = (e: any) => { const checkedValue = e.target.value; if (e.targ ...

AngularJS allowing multiple ngApps on a single page with their own ngRoute configurations, dynamically loaded and initialized

Seeking advice on lazy loading separate ngApps on one page and bootstrapping them using angular.bootstrap, each with its own unique ngRoute definitions to prevent overlap. I have a functioning plunkr example that appears to be working well, but I am unsur ...

Tips for eliminating the menu bar in the external window generated by an Electron application's URL

Looking for a solution to remove the menu bar from a window that opens with an external URL in an Electron application? Check out the code snippet below: windowObject.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrows ...

Is there a way for me to reach a parent instance of a class from a child instance?

I am currently working on a project that involves a class called "Main" with an instance named "main". This "main" instance includes two properties from other classes, referred to as "player" and "background". My goal is to have the child instances interac ...

Tips for incorporating a Forgot/Reset password option into your #Firebase platform

In the project I'm working on, I am utilizing #AngularFire2. My goal is to incorporate a Reset / Forgot password link into the login page. Does anyone have suggestions on how to accomplish this task? I'm looking to get some insights from #AskFi ...

Javascript clock problem, kick off on click

Currently working on a game and in need of a counter that starts when clicked and stops at 00 (From 1m to 00). It is currently starting onload and cycles back to start when it reaches 00. HTML: <body> <div> <div class="timer" >Battle t ...

Delete a specific character sequence from a PHP form before storing it in a MySQL database

My Objective: I aim to utilize a straightforward PHP form to transfer data to a MySQL database. However, in cases where the input includes a specific string, I intend to eliminate it before storing it in the MySQL database. For instance: if the input ...

What is the best way to align HTML inputs within a grid, whether it be done automatically or manually using JavaScript?

Arrange 20 HTML inputs with various data types into grid-columns as the user inputs data. Ensure that the grid container has div elements correctly positioned for output. ...

converting time stamps to written text

Is there a JavaScript library available that can convert a timestamp (Date Java class value) to text? For example: Just two minutes ago 4 hours back 23 August 2009 Does such a library exist? If not, I'll take on the challenge of implementing it ...

Is it possible to view the object sent from AJAX to PHP in PHP using a debugger?

I'm facing an issue where I am making an AJAX call to a PHP file, sending a JSON object as a parameter from JavaScript. The PHP file is supposed to perform some logic with the object and return it using json_encode('whatever');. However, the ...

substitute a variable

I am trying to update the value of a variable using an HTML form. Within my config.php file, I have declared a variable $num = 3; I would like the value 3 to be replaced with whatever I input in the form form.html. Below is the code for my HTML form : ...

Combining cells through the utilization of JavaScript

I've searched for ways to merge cells in a table using JavaScript but haven't been able to find any code that works. Is there a specific approach I can follow to implement cell merging like in MS-WORD tables? Your advice would be greatly apprec ...