Looking to switch up the background color while scrolling, any tips?

I'm trying to change the background color of my navbar section on scroll, but I've had no luck so far.

Below is the code I have been using:

const [colorChange, setColorChange] = useState(false);

const changeColor = () => {
   if (window.scrollY >= 120) {
     setColorChange(true);
   } else {
     setColorChange(false);
   }
}

Answer №1

Based on the code snippet provided, it appears that the changeColor method is not being called. Make sure to associate it with the onScroll event for proper execution.

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

Error: Django unable to load jQuery library

Hey there! I have a template that includes my JavaScript code. However, when I view the template in a browser, it doesn't provide the interaction I was hoping for. Upon checking the console, I see the following error messages: Error: Bootstrap's ...

Guide on dynamically updating a div in PHP with a mix of text output and images at regular intervals

My current project involves creating a browser-based card game primarily using PHP, with potentially incorporating other languages to help me enhance and test my PHP skills. However, I've encountered difficulties while attempting to implement various ...

Is there a way to execute a function after EACH user interaction?

I am in the process of developing a React Native kiosk application for an Android tablet that will be mounted on a wall. The main requirement is to include a "screen saver" feature, where if the user does not interact with the screen for 30 seconds, it wil ...

I am looking for a solution on how to validate a token issued by Auth0 in a nodejs backend using jwt, but I keep

My React frontend uses Auth0 for user authentication. Once a user is logged in, I retrieve the token using getAccessTokenSilently() and send it to the backend like this: const { user, isAuthenticated, getAccessTokenSilently } = useAuth0() useEffect(() =&g ...

An issue arises when attempting to integrate ajax with colorbox

I am currently facing an issue with a WordPress plugin designed for playing videos. It seems that the developer who created it is no longer available, and now the play video button has stopped working. Upon further investigation using Firebug, I noticed ...

The compatibility between Beautiful Dnd and react@18 seems to be causing some issues

Struggling to incorporate the gorgeous dnd package into my project, only to encounter an error during installation. Need assistance in finding an alternative package or resolving this issue. https://i.stack.imgur.com/cHiOb.png If anyone knows of a similar ...

A small computation

How can I modify this code to calculate the total price #ttc by multiplying #totalcout with #marge Currently, I am able to add amounts when checkboxes are clicked, but I am struggling with integrating the multiplication for calculating the Total Price (TT ...

Unable to modify the href of the footer icons on Pancakeswap

Hello all, I am new to working with React. I am a huge fan of the DeFi world, so I have decided to try forking the PancakeSwap frontend from GitHub. So far, I have been able to customize the styling, update the menu items, and more. However, I am facing a ...

Adjusting the viewbox size of an SVG image

Below is a code snippet that I need assistance with. In my production environment, I have two SVGs stacked on top of each other, each containing various content. When the user clicks on either side of the header displayed in the snippet, the SVG on top sho ...

Arranging a collection of objects (Displaying information in a random sequence)

Here is a summary of the data returned in a shortened version: var array = [{ "response": { "itineraries":[ { "price": { "totalPricePerPassenger":"104" ...

Customizing input tags

Greetings everyone, I have encountered a dilemma : <input type ="file" name ="fileUpload"> I created a file input that shows "choose a file" by default. I'm not too fond of it and would like to make it look more like a button. However, when I ...

What happens if I don't associate a function or method in the React class component?

Take a look at this straightforward code snippet that updates a count using two buttons with different values. import "./App.css"; import React, { Component } from "react"; class App extends React.Component { // Initializing state ...

Selenium unfortunately does not fully function with JavascriptExecutor

When I attempt to input text using JavascriptExecutor, the code snippet below is what I use: private void inputWorkDescription(WebDriver driver, int rawNumber) throws IOException, GeneralSecurityException { if (!getWorkDescriptionFromSheets(rawNum ...

Using a `right: 0` value will not be effective when using absolute positioning

Currently, I am in the process of developing a website utilizing Bootstrap 3.3.6. After going through a tutorial on how to create a responsive banner slider, I found this resource helpful: http://www.jqueryscript.net/slider/Full-Width-Responsive-Carousel- ...

What is the best approach for fetching a refined selection of items from an array of IDs using GraphQL?

If I have a list of products stored in my database, it might look something like this items: [ { id: '001', name: 'Product 01', description: 'Awesome product' price: 50.00 }, { ...

receiving unexpected data while using AJAX

For my PHP project, I have a function that validates a textbox using AJAX. The AJAX is working fine, but it only gives the following output for $return_value==-4 "<br /> <font size='1'><table class='xdebug-error xe-deprecated ...

Explain to me the process of passing functions in TypeScript

class Testing { number = 0; t3: T3; constructor() { this.t3 = new T3(this.output); } output() { console.log(this.number); } } class T3 { constructor(private output: any) { } printOutput() { ...

What is the best way to execute npm command in Visual Studio Code?

After successfully installing node.js on my local computer, I attempted to use the npm command in Visual Studio Code. Strangely, whenever I try a command like npm run start for React, I receive an error message stating: 'npm' is not recognized as ...

Is it possible to implement an SSL certificate with my Next JS deployment?

Currently, I am running a Next.js deployment on an EC2 instance and looking to secure it with an SSL certificate. My initial thought was to use a custom server config for this purpose, but I'm concerned that it may impact certain optimizations that I& ...

Error: The reduce function cannot be applied to $scope.array as it is not a

I am currently facing an issue with a section of my code that involves loading attributes related to a page using drop-down lists. These attributes, namely instruments, style, and scoring, are fetched through a service call. For instance, when retrieving ...