How can I apply the :hover effect to the .active class when my window has scrolled beyond 50 pixels?

Need to apply :hover when the .active class is added with the scrollTop() function > 50, as it removes the a:hover class.

Check out this code snippet on CodePen

.logo a {
  color: #000;
  font-size: 36px;
  text-decoration: none;
}

nav ul li a {
  display: inline-block;
  outline: none;
  color: #000;
  text-decoration: none;
  font-size: 36px;
}

nav ul li a.active {
  color: coral;
}
.logo a.active {
  color: coral;
}


$(window).on("scroll", function() {
  if ($(window).scrollTop() > 50) {
    $("nav ul li a").addClass("active");
  } else {
    $("nav ul li a").removeClass("active");
  }
});

$(window).on("scroll", function() {
  if ($(window).scrollTop() > 50) {
    $(".logo a").addClass("active");
  } else {
    $(".logo a").removeClass("active");
  }
});

Answer №1

It seems like the problem arises when the hover effect stops working after the styles change during scrolling. Below are the updated styles for your CSS along with added hover states for the .active a tags.

html {
  width: 100%;
  height: 100vh;
  margin: 0 auto;
  padding: 0 auto;
}

body {
  width: 100%;
  height: 100vh;
  margin: 0 auto;
  padding: 0 auto;
}

.header {
  position: fixed;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 85%;
  height: 80px;
}

.logo {
  font-family: "at";
}

a {
  color: #000;
  font-size: 36px;
  text-decoration: none;
  transition: color 0.6s ease;
}
a:hover {
  color: #ccc;
}
ul {
  font-family: "at";

  display: block;
  list-style-type: disc;
  margin-block-start: 0em;
  margin-block-end: 0em;
  margin-inline-start: 0px;
  margin-inline-end: 0px;
  padding-inline-start: 0px;
}

nav ul li {
  display: inline-block;
  font-family: "at";
  margin-top: 0px;
}

nav ul li a {
  display: inline-block;
  outline: none;
  color: #000;
  text-decoration: none;
  font-size: 36px;
}

.container {
  max-width: 100%;
  width: 85%;
  height: 76px;
  background-color: aqua;
  margin-left: auto;
  margin-right: auto;
}
.content-wrapper {
  max-width: 100%;
  width: 85%;
  height: 2000px;
  margin-left: auto;
  margin-right: auto;
  background-color: dimgray;
}
nav ul li a.active, .logo a.active {
  color: coral;
  transition: all 0.6s ease;
}

nav ul li a.active:hover, .logo a.active:hover {
  color: #ccc;
}

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 method for obtaining table structure using Bootstrap dropdowns?

[Using span instead of table element][1] [Using the table element][2] Currently working with Bootstrap 5, my goal is to have a fixed-width table in place of dropdown menu items. I aim to position the text in the 3rd column all the way to the right. The da ...

"Utilize the clickToggle or toggle functions in jQuery for seamless

Hello everyone, I've been working on a code to animate a box by toggling between show and hides states. However, I seem to be facing some issues and can't find the right solution. When I use a simple click function, it works fine, but I want to h ...

PHP allows for creating dropdown lists where the values are dynamically dependent on the selection of another dropdown list within the same form

Is there a way for me to implement this solution? The values in the dropdownlist are based on another dropdownlist within the same form. For example, a form with dropdownlists for car names and models of that car, along with a search button. Please take no ...

The mobile menu toggle functionality is malfunctioning on actual mobile devices, however, it is operational when tested on various mobile sim

Recently, I noticed that on , the menu collapses into a hamburger icon for mobile. However, when tapping on it on my phone and other devices, nothing happens. Surprisingly, it works perfectly fine when clicking on it in mobile simulators within Google Chro ...

AngularJS allows us to easily include the route provider templateUrl path in the view div, making

I have the following route provider setup: $routeProvider .when('/', { templateUrl: '/slapppoc/Style Library/TAC/Views/tarification/home.html', controller: 'homeController' /* resolve: { // This f ...

Floating a div while scrolling down until the footer is reached

I have a requirement to float a <div> while scrolling down, and it should stop when the <div> reaches the footer. Similarly, it should move back up when scrolling up. I managed to achieve this functionality using the following code: if (window ...

Using JavaScript or JQuery, is there a way to retrieve the favicon icon, Apple-touch icon, and title of a webpage?

Is it possible to extract just the favicon icon and title from a webpage using only an IFrame? For example, for a site like www.stackoverflow.com, I want to retrieve only its title and favicon icon/Apple-touch-icon. Are there any plugins or parsers avail ...

adjusting the font color based on the currently selected tab

After seeking help for my previous question on Unable to add a background colour to the button in navigation I have successfully resolved that issue and continued working, but now I am facing a new challenge. I need to change the font color of the navigat ...

What is the best way to apply a class to the currently active paragraph in Medium Editor?

Currently implementing the Medium Editor JS Clone in a project for article editing. The editor functions well, allowing element formatting with clean code. However, I am now exploring ways to add images during editing. An optimal solution would involve a ...

The jqxgrid from jqwidget is displaying all content in a single, messy column

After successfully publishing my project from ASP.NET MVC, everything was functioning perfectly. However, upon publication, the JQXWidget JqxGrid displayed data in a mixed-up manner as shown in this image: https://i.sstatic.net/0o1XY.jpg The layout of my ...

Leveraging CSS attribute selectors within JSX using TypeScript

With pure HTML/CSS, I can achieve the following: <div color="red"> red </div> <div color="yellow"> yellow </div> div[color="red"] { color: red; } div[color="yellow"] { color: yellow; ...

Formatting list items in HTML

I have just finished coding the following HTML code: My main goal is to align the list items - HOME | All About Kittens | Feeding Your Kitten in a proper layout. Currently, they are displayed as a simple list. However, I would like them to be aligned ...

Failure of knockout CSS binding to apply a class

I am attempting to assign a class dynamically based on a boolean value. Here is the structure of my code: <input data-bind=" css: { 'chkBxEdit': gThirdCheckboxEnabled(), 'chkBx': !gThirdCheckboxEnabled() ...

Having trouble getting my soundboard to work properly on mobile devices

I recently created a soundboard using HTML5, CSS3, and JavaScript. While the audio plays back perfectly on my computer when I click the buttons, I encounter an issue when trying to use it on a smartphone. <!DOCTYPE html> <html lang="en"> <h ...

Looking to update properties for elements within the Angular Material 16 mat-menu across the board?

Currently working with Angular Material 16 and I have a question regarding the height of buttons inside the mat-menu element. Here is an example of the code: <button mat-icon-button> <mat-icon>more_vert</mat-icon> </button> ...

What is the best way to modify the styling of my CSS attributes?

I'm currently working with this CSS code: input:checked + .selectablelabel .check { visibility: hidden; } Now, I want to change the visibility property to "visible" using JavaScript. I attempted the following: $(document).on('click', & ...

Creating and formatting HTML pages

Trying to expand my knowledge of HTML, I decided to experiment with some basic code. I selected what seemed like a straightforward layout from a website, saved the source onto my desktop, and made some slight modifications. When I opened the HTML file in a ...

Use the `document.getElementsByClassName` method to retrieve elements with a specific class name and then insert content between

Is it possible to select the only post with a 'selected' class and then insert a div inside or between other divs? I am currently uploading images to my website's timeline and now I am attempting to group multiple images posted on the same d ...

Unable to connect Dropzone to dynamically loaded DIV using AJAX

Using Dropzone for client-side image uploads has been a breeze. Take a look at this basic example that is currently up and running: If you examine the source code, you'll notice that I am utilizing JQuery to connect Dropzone to the upload1 div ID. H ...

Straightforward jQuery hover effect

I have created a simple jQuery image rollover function that was inspired by another one I found here: . This function identifies all images with "_off" in their name and swaps them with an image that has the same name but with "_on" instead of "_off". It& ...