The animated sticky header lacks smooth animation while scrolling upwards

This dynamic menu transitions the logo to the left and the navigation to the right as you scroll down. The animation is smooth when scrolling down, but slightly jerky when scrolling up, especially with the navigation.

The animation uses CSS3:

transition: all 0.4s ease;

JQuery is utilized only to add a class:

$(window).scroll(function() {
    if ($(this).scrollTop() > 1){  
        $('header').addClass("sticky");
    }
    else{
        $('header').removeClass("sticky");
    }
});

I believe the issue lies within the CSS, but I'm having trouble pinpointing it. See the code in action on Jsfiddle here. Any assistance would be greatly appreciated. Thank you.

Answer №1

Your CSS transition effect is specifically set up for the header links only when their parent element has a "sticky" class attached to it. This means that once the "sticky" class is removed, the transition effect does not take place.

To resolve this issue, ensure that the transition effect is also applied to the <a> elements that do not have the "sticky" class, similar to how you have implemented it for your <h1>.

Replace your current CSS setup with this revised version:

nav ul li a {
  ...
  -webkit-transition: all 0.4s ease;
  transition: all 0.4s ease;
}

.sticky nav ul li a { ... }

An illustration of the problem and solution can be seen below:

$(window).scroll(function() {
  if ($(this).scrollTop() > 1) {
    $('header').addClass("sticky");
  } else {
    $('header').removeClass("sticky");
  }
});
body {
  line-height: 1;
  font-family: 'PT Sans', sans-serif;
  margin: 0;
}
ol,
ul {
  list-style: none;
}
img {
  max-width: 100%;
}
/* NAV */

header {
  position: fixed;
  width: 100%;
  background: #335C7D;
}
header h1 {
  font-size: 40px;
  color: #fff;
  -webkit-transition: all 0.4s ease;
  transition: all 0.4s ease;
}
.sticky h1 {
  font-size: 25px;
}
.logo {
  float: left;
  display: block;
  padding: 20px;
}
nav ul {
  float: right;
  position: relative;
  top: 30px;
  right: 20px;
}
nav ul li {
  float: left;
}
nav ul li a {
  padding: 10px;
  font-size: 30px;
  text-decoration: none;
  color: #fff;
  display: block;
  -webkit-transition: all 0.4s ease;
  transition: all 0.4s ease;
}
.sticky nav ul li a {
  font-size: 20px;
  position: relative;
  top: -5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<header>
  <span class="logo">
    <h1>Sticky Header</h1>
  </span>

  <nav>
    <ul>
      <li><a href="#">Page 1</a>
      </li>
      <li><a href="#">Page 2</a>
      </li>
      <li><a href="#">Page 3</a>
      </li>
    </ul>
  </nav>
</header>

<!-- an image for demonstration purposes -->
<img src="http://netdna.webdesignerdepot.com/uploads7/how-to-create-an-animated-sticky-header-with-css3-and-jquery/large-image.jpg" alt="Big Image" />

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

The background color is failing to respond to the padding of the adjacent image

Check out this page to see the issue. The code snippet surrounded by pre tags has a background-color that is not aligning properly with the image, even though the image has a defined padding of 16 pixels on the right. The text alignment is correct, but the ...

Instructions for adding a new line in a textarea on Internet Explorer when reading from a file

I'm facing a situation where I need to display the contents of a file (let's call it abc.txt) in a textarea on a JSP page. However, when I try to do so, all the contents are displayed in one line without any line breaks or carriage returns. Inte ...

Conceal a particular image within a designated group

A task I have is to execute a hide function followed by a deletion script when the user interacts with an image. Here's the code snippet for displaying the image: <?php echo '<img src="../uploads/foto/clanky/' . $file . '" id="& ...

How to display an unordered list horizontally in HTML

I'm working on a screen that has filters displayed vertically, but I want to align them horizontally and have the filter options arranged in two columns. I've tried using CSS properties like spacing, clear, and display block, but the checkboxes/l ...

Utilizing jQuery to apply a class to a targeted element when clicked

I'm currently working on animating a menu item where it expands to the left when hovered over, and contracts back when the mouse moves out. This part is functioning as expected. Additionally, I am trying to apply a specific color to the button by add ...

Using CSS to disable an image map in conjunction with dynamically adjusting CSS styling

On the desktop version of the website, there is an imagemap implemented. However, when the site transitions into tablet or mobile view, the imagemap should be disabled to allow for a simpler navigation area to be displayed. I attempted to use the following ...

End the animation in jQuery

I am encountering a problem with a code snippet like this: $("#clickme").click(function () { $("#status").fadeIn(250).delay(2500).fadeOut(250); }); This code displays a message after a user successfully saves some information. The issue arises wh ...

Tips for adjusting your Navbar from transparent to solid based on screen size changes instead of scrolling

I am seeking assistance in creating a responsive Navbar that transitions from transparent to solid when the screen size changes, such as on a smartphone. I want the Navbar to remain fixed at the top of the page and not move down as I scroll. I tried adding ...

The SVG image does not display in browsers other than Internet Explorer (IE)

I am encountering an issue with adding a menu toggle image in SVG format to my HTML. Unfortunately, the image is not displaying as expected. Here are screenshots for comparison between Explorer and Chrome: https://i.stack.imgur.com/74sqh.png https://i. ...

Pass data from Action Class to ajax using JSON syntax

Currently I am facing an issue while working on fullCalendar, a jQuery plugin. I am trying to populate event details from a database when the Calendar page loads, but I seem to be stuck with a silly problem that I can't figure out. $('#calendar& ...

Trouble accessing JSON data from Webapi using JQuery and displaying it in an HTML table in an ASP.NET environment

I've been struggling with this issue for some time now and I just can't seem to make any progress. It's currently not functioning at all, and I'm unsure of where I went wrong. Sample JSON data: {"$id":"1","content":"hello","fname":"Em ...

"The issue seems to stem from a snippet of compact JavaScript

I am facing an issue with a list item that contains both an image and text. The text is overlapping the image, but when I hover over the text, it disappears and only the picture is visible. Here is the HTML code snippet: <ul> <li><img ...

Significant empty space to the left of content due to Zurb Foundation

I'm currently utilizing Zurb Foundation within a rails project. My goal is to structure a basic page that consists of a table, text field, and a couple of buttons. However, I am facing a significant gap on the left side of my content, causing the rig ...

Ensure that the div spans the entire width of the page, prioritize maintaining the aspect ratio, but allow for variable

I have a dilemma with a div containing multiple elements. My goal is to make the div 100% in width, while also attempting to preserve the aspect ratio if feasible. Should the enclosed elements exceed the div's boundaries, then the height will adjust a ...

Difficulty encountered when loading JSON data using the getJSON function

When I open the JSON file in my web browser and input this code into the console: var p = document.getElementsByTagName('pre'); for(i=0; i < p.length; i++) { var data = JSON.parse(p[i].innerHTML); var pc = data.postalcodes; for (va ...

Obtain the mean value by selecting an input radio button

My goal is to calculate the average based on the selected radio button values. I have a form with 30 radio buttons, each ranging from 1 to 10. Now, I need to compute the average score and display it as a final result. While I managed to achieve this ...

Tips for transferring the anchor value using jQuery AJAX to PHP

I have been attempting to use jQuery to send the values of a couple of anchors to a PHP file, but I am not receiving any callback from the PHP script. <div class="result"></div> <a href="#" value="5" class="star">Star 5</a> <a ...

Avoid using the jQuery.get() function

$.get('./mods/webim_offline_email.php', {name: $('#webim_name').val(), email_address: $('#webim_email_address').val(), msg: $('#webim_msg').val()}, function (data){ alert(1); $('#webim_offline_form' ...

Utilize JQuery to Extract JSON Data from Django

How can I properly transfer data results from Django to JavaScript in JSON using an AJAX call? In Django, I currently have the following variable: results = {key1:[1,2,3], key2:[[name1,lat1,lng1],[name2,lat2,lng2],[name3,lat3,lng3]]} After suc ...

Send information to modal using Javascript

I am facing an issue when trying to transfer data from JavaScript to a Bootstrap modal. Here is how my code looks: Modal: <div id="myModal" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> ...