What adjustments should be made to my jQuery code in order to achieve a smooth transition effect?

I'm currently utilizing JQuery to make CSS adjustments on my website and encountering issues with some of the code. I am still new to using jQuery, but usually, I can troubleshoot when making changes - however, this particular issue has me stumped.

Below are fragments of my HTML, CSS, and JQuery. My goal is to underline a link on hover while also changing the background and link color when a user scrolls down the page. Everything works as expected except for figuring out how to make the underline appear when scrolling occurs.

Any assistance would be greatly appreciated. I understand that the solution is probably simple, but I'm unsure about what exactly needs to be changed in my JQuery code.

I intentionally omitted the non-working code from the snippet, but I suspect I need to include additional statements within the if statement in the JQuery section. I've been attempting to insert code there, but nothing seems to work.

Additionally, I almost forgot to mention that the current code underlines the link on hover (in the same color as the link). I want to replicate this behavior when the user scrolls down - if the link is typically blue with a white background, the hover underline should also be blue; after scrolling down, the background becomes blue and the link turns gold. I aim for the underline to be gold too.

Thank you,

$(window).scroll(function() {
  // 100 = The point you would like to fade the nav in.

  if ($(window).scrollTop() > 50) {
    $('.bg').addClass('show');
    $('nav li a').css('color', '#FFCF01');
  } else {
    $('.bg').removeClass('show');
    $('nav li a').css('color', '#00529C');

  };
});

$('.scroll').on('click', function(e) {
  e.preventDefault()

  $('html, body').animate({
    scrollTop: $(this.hash).offset().top
  }, 1000);
});
nav {
  margin: 0 auto;
  max-width: 1024px;
}

.hdrmenu ul {
  margin-right: 1em;
  padding-top: 1.25em;
}

.hdrmenu li {
  float: left;
  list-style: none;
}

.hdrmenu li a {
  text-align: center;
  text-decoration: none;
  padding: 0em 1em;
  overflow: hidden;
  color: #00529C;
}

.show {
  background-color: #00529C;
  color: #FFCF01;
  width: 100%;
  height: 6em;
  opacity: 0.8;
}

.slide-left-right {
  text-decoration: none;
  display: inline-block;
  color: #FFCF01;
  /* yellow gold */
  float: left;
}


/* add a empty string after the elment with class .slide-left-right */

.slide-left-right:after {
  content: '';
  display: block;
  height: 2px;
  width: 0;
  background: transparent;
  transition: width .7s ease, background-color .7s ease;
  /* .5 seonds for changes to the width and background-color */
  -webkit-transition: width .7s ease, background-color .7s ease;
  /* Chrome and Safari */
  -moz-transition: width .7s ease, background-color .7s ease;
  /* FireFox */
}


/* Change the width and background on hover, aka sliding out */

.slide-left-right:hover:after {
  width: 100%;
  background: #00529C;
  /* lc blue */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<header class="bg transition">
  <nav class="hdrmenu">
    <a href="index.html"><img src="_images/logo.gif" class="logo"></a>
    <ul style="float: right;">
      <li><a href="one.html" class="slide-left-right">Diabetes</a></li>
      <li><a href="two.html" class="slide-left-right">Vision</a></li>
      <li><a href="three.html" class="slide-left-right">Hunger</a></li>
      <li><a href="four.html" class="slide-left-right">Environmental Issues</a></li>
      <li><a href="five.html" class="slide-left-right">Childhood Cancer</a></li>
      <li><a href="six.html" class="slide-left-right boldLi">We Serve</a></li>
    </ul>
  </nav>
</header>

Answer №1

Give this a shot.

 if ($(window).scrollTop() > 50) {
     $('.bg').addClass('show');
     $('nav li a').css('color', '#FFCF01');
     $('nav li a').css('text-decoration', 'underline');
 } else {
     $('.bg').removeClass('show');
     $('nav li a').css('color', '#00529C');
     $('nav li a').css('text-decoration', 'none');
 };

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

Does a CSS selector exist that targets every single element on a webpage?

Knowing about the asterisk as a wildcard in CSS is not new to me. My query is: Is there a CSS selector that targets every single element on a web page, including html, body, and all descendants? I have made attempts like: * { color: green; /*I used ...

Utilizing the power of jQuery within three.js

Thank you once again for your previous assistance, but I find myself in need of your expertise once more. I have successfully added markers to my map as desired. However, these markers now require functionality to be clickable. Specifically, when clicked, ...

Different ways to rearrange the placement of Export buttons in a personalized div or outside the table using Datatable

I would like to display the export buttons outside of the table. While searching on stackoverflow, I came across an example that uses the Select options method. You can find the example here. If anyone knows how to achieve this, please modify it and shar ...

Center the div vertically

How can I vertically center a div next to a tall image using CSS? The left column (#watch) is an image and the right column (#watch-column) is a div that needs to be centered. I've tried different methods but nothing seems to work with my somewhat flu ...

Unable to generate a menu with bootstrap

Hey there, I've been experimenting with creating a responsive menu using Bootstrap, but I'm struggling with the implementation. My vision is to have a logo on the left side and a menu on the right side. Below is the concept for my menu design. ...

JavaScript and HTML are commonly used programming languages for developing

By utilizing JavaScript, I was able to generate a table dynamically based on user input. For example, if the user enters 3 and clicks "go", a table with 3 rows is created. Using the .keyup function allowed me to target a column successfully. However, an i ...

Tips on connecting a font directory from FontAwesome

I am currently attempting to incorporate the fonts provided by FontAwesome for their icons, and they specify that a root folder named Fonts is needed. My attempt to link it was using: <link type="text/css" href="/myPath/font" /> ...

The Ion slider's onFinish event is triggered when the page loads, independent of any user interaction with the slider

I am facing an issue with ionslider. I have observed that the plugin triggers the onFinish action without any user interaction when the page is loaded. However, on the demo site ionden.com/a/plugins/ion.rangeSlider/demo_interactions.html, the plugin only r ...

Alternate Row Colors Using Odd/Even CSS Styling for Main Headings

In my expand/collapse table, I have set up alternating row colors (dark grey and light grey) that adjust automatically when expanding or collapsing. The challenge I'm facing is that for certain rows, I want to apply a specific background color using ...

Tips for sorting through array information by utilizing JQuery Date?

In the view page, I am displaying this array within a <div>. Now, I need to filter this array based on the date key. The array structure is as follows: array:5 [▼ 0 => array:20 [▶] 1 => array:20 [▶] 2 => array:20 [▶] 3 => ...

What is the best way to display my icon <div>s side by side instead of on top of each other?

I am facing an issue with the following code: <a href="#"><div class="iconFriends"></div></a> <a href="#"><div class="iconFavorite"></div></a> <a href="#"><div class="iconPM"></div></a&g ...

How to use jQuery to retrieve the style of an element based on a specific data attribute

I've been using bxSlider and I decided to create a unique custom pager using the pagerCustom option. My goal was to make the pager resemble a thumbnail pager, so I attempted to copy the style of each slide and attach it to the corresponding pager. For ...

Encountering a problem with AJAX 'post' method while trying to post HTML elements

Incorporating AJAX to transmit the content of a div to my C# MVC controller. The structure of the AJAX post is as follows: $.ajax({ url: '/Home/SaveContent', type: 'POST', data: { model: modelDataJson, activ ...

Send a parameter to an Angular directive when clicked

I am working on a directive that will allow me to retrieve parameters upon clicking. I need to access the child data within the click event to determine if it has children or not. ..... html div ng-app="treeApp"> <ul> <treeparent>< ...

The specified anti-forgery form field named "__RequestVerificationToken" is not found

Dealing with cross-site attacks is a priority for my site. Currently, I am working on developing a Master/Detail form using asp.net mvc 5 and making Ajax requests. To create a new entry, I have to follow a process involving an Ajax Request like this: $.aj ...

Consistent column heights within each row

I currently have Bootstrap implemented on my website and have integrated jQuery to adjust the height of each column to match the tallest one. However, the issue I am facing is that the height adjustment applies globally across the entire page, rather than ...

What is the best way to limit the size of a scrollable list while also allowing it to shrink when there is not enough content?

I'm currently developing a details page that is pulling information from a database. This details page has the potential to display related data from two other tables, each of which could have zero or multiple records to show. The layout I am using ( ...

The functionality of the Checkbox is experiencing issues when used in conjunction with Bootstrap 5 and Vuejs

Here is a div showcasing an input with its label. However, there seems to be an issue where I am unable to click on the checkboxes. This code snippet serves as an illustration of utilizing Bootstrap 5 checkboxes within VueJs. <ul class="widget-lis ...

Ways to disable autofocus on the initial input element when clicking a hyperlink within a jQuery Mobile popup

Currently, I am facing a situation where I have a textarea and a link inside a jQuery Mobile popup. Whenever I click on the link, the textarea automatically gets focused and the keypad pops up (on iOS). However, what I actually want to do is redirect to an ...

In what way do Tumblr and Google+ creatively arrange images like a jigsaw puzzle?

I'm interested in creating a unique gallery of images and I'm curious about how to stack them similar to Google and Tumblr. What I mean is, on an archive page of Tumblr, the images are stacked in columns like this: However, I also want the imag ...