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

Encountering difficulty inserting ajax response into a specific div element

What could be the issue? I've included the getElementById and I am receiving a response, as confirmed by Firebug. The response is correct, but for some reason it's not populating my div area. <script> $(document).ready(function () { $( ...

What is the best method to extend my borders to the very edge?

Struggling with table borders extending to the edge on a website using master pages and CSS stylesheet. This issue requires some troubleshooting, so any help would be appreciated. The problematic website is here. I need the border under the banner menu to ...

How to target a class with a specific number in CSS selectors

My mobile hybrid application is built with Sencha Touch 2 and requires some customization based on the iOS version it's running on. In my Sass stylesheet, I originally had the following selector: .x-ios-7 { /* add iOS7 customizations here */ } How ...

Select a date from the JQuery calendar, verify it against the database, and display any events scheduled for that date

I am working with a jQuery calendar that stores the selected date in a variable named "X". My goal is to retrieve events stored on that specific date from the Database and display them. Can anyone provide some guidance? <div id="calendar"></div&g ...

Is there a way to showcase posts with a certain tag in a single column on my tumblr theme, while reserving the other column for posts aimed at the wider audience?

I am interested in customizing my theme so that one section on the homepage of my blog only shows posts with a specific tag (such as "mine" or "personal"), while another section displays all posts, regardless of tags. Although I have searched through simi ...

The functionality of display flex is not functioning as expected outside of the CodePen

I attempted to create a responsive two-column layout on Codepen with success. The layout is quite simple, featuring a YouTube video and some text. However, when I tried to transfer this to my website, it failed to work... Here is the code – hoping someo ...

Implementing modifications to all HTML elements simultaneously

In my HTML document, there are around 80 small boxes arranged in a grid layout. Each box contains unique text but follows the same structure with three values: name, email, and mobile number. I need to switch the positions of the email and mobile number v ...

What is the reason behind the shifting behavior of e.currentTarget when using event delegation in jQuery?

Click here for the code snippet <div id="container"> <button id="foo">Foo button</button> <button id="bar">Bar button</button> </div> $('#container').on('click', function(e) { var targ ...

Sending a list of custom objects including images via AJAX to a REST API in a Spring Boot application

Encountering a problem with an Ajax call when passing a List of custom objects with images to a REST API in Spring Boot. Below is a snippet of my code. Controller @PostMapping(value = { "/test/saveUser" }, consumes = { "multipart/form-data& ...

Issue: Stylesheet not being loaded on Index.html through Gulp

Issue The .css file is not being loaded by the index.html despite setting up a gulp.js file. It seems like the folder directory might be causing the problem. However, the .scss files in the /src/scss folder are compiling correctly into CSS within the /bui ...

What could be causing the disappearance of my <Div> when applying a Class to the contained <span>?

My goal is to showcase a variable on my HTML page using CSS for a Graphical display in OBS streaming software. Whenever I apply the class .wrappers to the span id p1Name (and p2Name), the text disappears from the HTML output in OBS. I'm puzzled as to ...

Static list elements are utilized in the CSS drop-down menu

It may appear that my coding skills are lacking or incorrect. When I attempt to create a drop-down menu in CSS, the new list elements end up on the other side of the page instead of within the container box. How can this be fixed? Below is the code snippe ...

What is the best way to create an associative array using jQuery and then send it through AJAX to be parsed by PHP?

Is there a way to create an associative array in jQuery and send it via ajax to a php page for processing? Here is an example of what I am trying to achieve... // jQuery if($something == true) { data[alt] = $(this).attr('alt'); data[sr ...

Does Google Chrome have a strange way of interpreting CSS?

Almost done with my website and it looks great on Firefox and IE 8, but Chrome is causing issues. Here's how it appears on FF: And here's Chrome: Notice how Chrome resizes the first image even though the code is the same as the one below? Che ...

Unable to apply box-shadow styling to input elements

Why am I having trouble adding box shadows to input elements in my CSS? I've tried something like this http://jsfiddle.net/DLCTh/, and while it applies correctly to div elements, it doesn't seem to work on text inputs. Am I overlooking something ...

Choose the initial division within the table and switch the class

Here is a straightforward request - I need to employ jquery to target the first div with the class of "boxgrid captionfull" within the tr element with the classes "row-1 row-first," and switch the class to 'active_labBeitrag'. <table class="v ...

iterate through the ordered list (ol) elements within nested lists

I am currently attempting to convert my hierarchical list into a JSON object in the following format; [ {title: title1,id: 1,children: [ {title: title1_1,id: 1_1,children: [ {title: title1_1_1,id: 1_1_1,children: []}, ...

Using a jQuery AJAX request to update the quantity of available items based on user input

Is there a way to use jQuery to capture user input values and product value, then send it to the backend? I need this to trigger an AJAX call that will hit a Java backend. The goal is to provide a product number and quantity value in order to generate th ...

Using div elements to mimic the layout of tables with row spans

<div class="container"> <div class="box1">1</div> <div class="box2">2</div> <div class="box3">3</div> </div> .box1 { border: 1px solid red; float: left; width: 20px; } .box2, .box3 { ...

When the child content surpasses the height of the parent, you can scroll within an overflow:visible; div

My sidebar navigation menu includes children and sub-children that are revealed on hover. You can view a simplified version of it in this jsfiddle link: https://jsfiddle.net/s096zfpd/ Although the example provided is basic, my main concern arises when the ...