Scrolling animations do not support the Translate3d feature

I'm struggling to implement a smooth scroll effect on the header of my website. My approach involves using transform:translate3d for animation, with the goal of keeping the header fixed at the top and moving it out of view as the user scrolls down. I have added classes for fixed positioning and sliding down into view to achieve this effect.

However, when scrolling down, instead of a seamless motion, the header seems to jerk into view or even bounce up and down when scrolling slowly. This unexpected behavior has me puzzled.

Any insights on why this might be occurring?

jQuery(document).ready(function($) {

      $(window).scroll(function() {

      if ($(this).scrollTop() > 75) {
           $('body').addClass('scroll-1');
    } else {
      $('body').removeClass('scroll-1');
    }

      if ($(this).scrollTop() > 100) {
      $('body').addClass('scroll-2');
    } else {
      $('body').removeClass('scroll-2');
    }
  });

});
html {
     position:relative;
     height:100%;
     background-color:darkorange;
}

 body {
      min-height:100%;
      margin:0; 
      padding:0; 

      display:-webkit-box;
      display:-webkit-flex;
      display:-ms-flexbox;
      display:flex;

    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -webkit-flex-direction: column;
    -ms-flex-direction: column;
flex-direction: column;
}

.header {
      position:absolute;
      height:50px;
      width:100%;
      background-color:transparent;
      border-bottom-color:black;
      border-bottom-width:2px;
      border-bottom-style:solid;
      transition: all .5s;
}

 body.scroll-1 .header {
      position:fixed;
      background-color:#fff;
      transform:translate3d(0px,-50px,0px);
      transition: background-color 0s, transform .6s;
}

 body.scroll-2 .header {
      transform:translate3d(0px,0px,0px);
      transition: background-color 0s, transform .6s;
}

 ul.menu {
      display:inline-block;
}

 ul.menu li {
      color:green;
      display:inline-block;
      margin: 0px 20px;
}

.content {
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    -ms-flex: 1;
      flex: 1;
      width:85%;
      height:1000px;
      margin-top:80px;
      margin-left:auto;
      margin-right:auto;
      padding-top:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<header class="header">
      <ul class="menu">
          <li>Link 1</li>
          <li>Link 2</li>
          <li>Link 3</li>
      </ul>
  
 </header>

 <div class="content">
</div>

jsfiddle

Answer №1

The header may appear jerky because the animation involves changing two CSS attributes: position to fixed and translate3d to maintain it at 0px. Unfortunately, animating changes to the position attribute is not supported by CSS3 animations. The element is simply transitioned from its start to end position within the specified time duration. In this scenario, the translation starts and ends at 0px, but the visible change in the header's position from absolute to fixed is not smoothly animated by the browser.

You can test the effect using this JSFiddle link.

JavaScript Code:

jQuery(document).ready(function($) {
  $(window).scroll(function() {
    if ($(this).scrollTop() > 100) {
      $('body').addClass('scroll');
      $('body').removeClass('fixedPos');
    } else if ($(this).scrollTop() > 75 && $(this).scrollTop() < 100) {
      $('body').addClass('fixedPos');
      $('body').removeClass('scroll');
    } else {
      $('body').removeClass('fixedPos');
      $('body').removeClass('scroll');
    }
  });
});

CSS Code (only modifications)

.header {
  position: absolute;
  height: 50px;
  width: 100%;
  transform: translate3d(0px, 0px, 0px);
  background-color: transparent;
  border-bottom-color: black;
  border-bottom-width: 2px;
  border-bottom-style: solid;
}

body.fixedPos .header {
  position: fixed;
  transform: translate3d(0px, -50px, 0px);
}

body.scroll .header {
  position: fixed;
  background-color: #fff;
  transform: translate3d(0px, 0px, 0px);
  transition: background-color 1s, transform 1s;
}

Answer №2

Jack recommends using this particular JavaScript script along with frame animation for optimal results.

$(window).scroll(function() {
      if ($(window).scrollTop() > asmhObject.sticky_scroll_position) {
        header[0].style.top = admin_bar_height + 'px';
        header.addClass('stick');
        var max_width = header.find('.middle .container').css('max-width');
        if (max_width !== 'none') {
          header.find('.middle .container').css('width', max_width);
        }

      } else {
        header[0].style.top = -header.height() + 'px';
        header.find('.secondary.dropdown > .sub-menu').removeAttr('style');
        header.removeClass('stick');
      }
    });
  }

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

Revamping the hyperlinks in my collapsible menu extension

Is there a way to modify the links in this accordion drop menu so that they lead to external HTML pages instead of specific areas on the same page? I've tried various methods but can't seem to achieve it without affecting the styles. Currently, i ...

Adjust image loading according to screen dimensions

I am working on HTML code that currently displays an image. The code looks like this: <div> <img id="wm01" alt="PP" title="PP" u="image" src="theImages/wm01.jpg" /> </div> My goal is to show a different image based on the screen si ...

Tips on concealing the visibility of a table row using PHP

My HTML table structure is quite simple: <style> .demo { width:100%; border:1px solid #C0C0C0; border-collapse:collapse; padding:5px; } .demo th { border:1px sol ...

PHP jQuery buttons for popovers

With a simple click of a button, I can generate 8 presentations and then edit each one individually by clicking on its respective name. Within this editing process, there is another form where I would like to include additional buttons that allow me to cus ...

Utilizing JavaScript for the removal or hiding of span elements with specific class attributes

I am currently working on a software project that involves compiling HTML fragments and then exporting them to Microsoft Word. My goal is to create a script that will cycle through these compiled fragments and remove specific tags that have a particular CS ...

Ways to avoid Parents Click Event from triggering during a Child's (Grandchild's) click event

My parent div has a unique feature where clicking on it reveals one of the child divs, and clicking again toggles the visibility of the child div. Inside this child div, there is a switch toggle that functions as a hidden checkbox. The issue I'm facin ...

Eliminate extraneous space with the clearfix:after pseudo-element

Could use some help figuring out how to remove the unwanted whitespace caused by clearing the float (specifically after the tabs). Any suggestions on how to fix this issue? Take a look at the code snippet below (jsfiddle): /* Clearfix */ .clearfix:af ...

Is there a way to create automatic line breaks in this text?

Is there a way to automatically ensure the span spans multiple lines? I am working with HTML and CSS. Below is a modified version of my code, as some parts are in PHP: <div style='border: 1px solid; padding: 10px; margin-top: 5px;'> < ...

How can I implement a sliding page effect using jQuery with a single navigation bar?

I came across a helpful tutorial at this link: I really admire the sliding effects on this website: Is there a way to achieve similar sliding effects like dk new media's, but with just a single navigation bar? ...

The stacking order of a child element within a parent element, which has a transform

Hey there, I've encountered a problem and was wondering if you could assist me with it. In the code snippet provided below, you'll see that there are elements with: transform: translate(0,0); Within these elements, there is a "dropdown" elemen ...

Tips for utilizing a template while inserting a new row into a Kendo UI grid

When attempting to add a new row to a kendo grid, I encountered an issue with inserting a dropdown template in one of the cells. The current method I am using to add a new row is as follows: grid.dataSource.insert(0, { AreaID: null, AreaName: "New Area", ...

Refresh the table data dynamically without the need for page reloading

Looking for a way to update data in a table without refreshing the entire page? Check out this example: Here is the form: https://i.stack.imgur.com/ISQbC.png After clicking on Update All, only the first row gets updated, not the subsequent rows. <?ph ...

What could be the issue with my JSON file?

I am currently utilizing the jQuery function $.getJson. It is successfully sending the desired data, and the PHP script generating the JSON is functioning properly. However, I am encountering an issue at this stage. Within my $.getJSON code, my intention ...

Mastering the art of maximizing efficiency with the Jquery Chosen Plugin

Currently, I'm facing an issue with the jQuery chosen plugin due to my large datasets causing the select box to hang and slow down. Below is my implementation of the plugin: var request = $.ajax({ method: "POST", url: "ajaxRequest.php", d ...

Check the File Format (not just the Extension) prior to Uploading

When uploading a file within an HTML form, you can validate the extension using JQuery's Validation plugin: <form enctype="multipart/form-data" id="form-id"> <input type="file" name="file-id" id="file-id" /> </form> To validate ...

Guide on incorporating jQuery library files into existing application code with the npm command

Recently, I used a node JS yo ko command to create a single-page application using Knockout-JS. Following that, I proceeded to install jquery packages using the command npm install jquery The installation was successful. However, my current goal is to in ...

Generating, establishing aesthetics for a specific span

My goal is to automatically detect addresses within a page and apply the class "address" where they are found. var addressPatternApplier = function(element, pattern, style) { var innerText = element.innerText; var matches = innerText.match(pattern); ...

Colorbox offers the ability to display or mimic a 'loading' animation for content that is displayed inline

Hello! I am currently utilizing ColorBox and implementing a few steps in a form using jQuery. We are looking to display the loading animation of ColorBox while waiting for responses from AJAX requests. Could you please provide guidance on how we can ac ...

Acquiring variables from a JQuery Ajax request while invoking a JavaScript file

I'm currently experimenting with using JQuery Ajax to retrieve a javascript file. I need to pass some parameters to it, but I'm not entirely sure how to do so in Javascript (PHP seems easier for this). Here is my current setup: function getDocum ...

Learn how to send multiple checkbox values using jQuery and AJAX requests

When trying to extract the value from multiple checkboxes, I utilize this particular code snippet: <form class="myform" method="post" action=""> <input type="checkbox" class="checkbox" value="11" /><br> <input type="ch ...