Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sticky navigation element jumps during scroll

Tags:

In Firefox especially, I've run into an issue I can't figure out how to fix.

On the following page, when scrolling down the page jumps several times - mainly on smaller screens where the page doesn't have its full size displayed. You can replicate this issue by making your browser smaller than the page so you have to scroll.

It's on this page: http://www.nucanoe.com/frontier-accessories/

If I disable the position:fixed on the navigation selector, it fixes the issue - but we need the navigation to be sticky. Is there a solution to fix this? I'm thinking we may need to use jQuery somehow.

Thanks in advance!

like image 655
Aaron M Avatar asked Mar 04 '15 20:03

Aaron M


People also ask

Why does the page jump when I scroll down?

On the following page, when scrolling down the page jumps several times - mainly on smaller screens where the page doesn't have its full size displayed. You can replicate this issue by making your browser smaller than the page so you have to scroll.

What are some weird scroll behaviour with jQuery animate?

1 Vertical scroll jumps when menubar fixes to top of screen 1 Navbar is bouncing on scroll when getting "stickied" to the top 0 Weird scroll behaviour with JQuery animate Related 4751 How to horizontally center an element

How to fix my navigation bar jump not working?

How To Fix You can get around this by wrapping your navigation element in a new div- let's call it nav-wrapper - and set its height to the same as your navigation element. These are known as placeholder elements. This new wrapper and your original navigation bar must always be the same height for the 'jump' to disappear.

How do I hide the jump to the navigation bar?

You can get around this by wrapping your navigation element in a new div- let's call it nav-wrapper - and set its height to the same as your navigation element. These are known as placeholder elements. This new wrapper and your original navigation bar must always be the same height for the 'jump' to disappear.


1 Answers

After seeing you asking for help on another answer, I will try and explain more clearly for you.

The Problem

Your problem is when you add position:fixed to the navigation bar, it removes it from its place and sticks it at the top of the page. This is why the rest of your content jumps up - because the navigation bar is not where it was anymore.

How To Fix

You can get around this by wrapping your navigation element in a new div - let's call it nav-wrapper - and set its height to the same as your navigation element. These are known as placeholder elements. This new wrapper and your original navigation bar must always be the same height for the 'jump' to disappear.

<div class="nav-wrapper" style="height:80px;"> <-- add this      <div class="your-original-nav" style="height:80px"></div>  </div> <!-- add this 

Now, when you set the navigation bar to fixed and it disappears to the top, the new wrapper we created with the same height keeps the page's content the same. When the fixed class has been removed, it sits back in the wrapper again, without pushing the content down.

A Suggestion

From what I can see on your site, there will be a big gap where the navigation bar was until the new fixed navigation reaches that point and covers it. What you want, is a little jQuery to figure out where to make the navigation fixed and where to hide it. I'll explain:

// cache the element var $navBar = $('.your-original-nav');  // find original navigation bar position var navPos = $navBar.offset().top;  // on scroll $(window).scroll(function() {      // get scroll position from top of the page     var scrollPos = $(this).scrollTop();      // check if scroll position is >= the nav position     if (scrollPos >= navPos) {         $navBar.addClass('fixed');     } else {         $navBar.removeClass('fixed');     }  }); 

You may want to add further functionality to this example, as it is very, very basic. You would probably want to recalculate the offsets on window resize as one addition.

A Demo

This is a little demo which might help you - I was bored and feeling helpful :)

like image 87
TheCarver Avatar answered Jan 21 '23 23:01

TheCarver