Strangely glitchy navigation bar using jQuery

Over at this website, there's a top navbar that shrinks using jQuery when scrollTop exceeds 100px. The functionality works as intended, but there's an annoying stutter issue where the navbar starts jumping up and down erratically after slight scrolling past the threshold. I'm stumped on what's causing this problem and how to resolve it. Any suggestions?

$(document).on("scroll", function() {
  if ($(document).scrollTop() > 100) {
    $("header").addClass("shrink");
  } else {
    $("header").removeClass("shrink");
  }
});
header.is-fixed {
  position: sticky;
  z-index: 9999;
  left: 0;
  right: 0;
  top: 0;
  padding: 1.5rem 0;
  transition: 0.2s;
  background-color: #333;
}

header.shrink {
  padding: 0.75rem 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="is-fixed">
  <div class="container">
    <nav class="navbar" role="navigation" aria-label="main navigation">
      Content here
    </nav>
  </div>
</header>
<div style="height:2000px; background-color:red">
</div>

Answer №1

It is important to note that this bug is more likely to occur when using a highly sensitive scroll peripheral, such as a Mac trackpad. Using a standard mouse wheel can greatly reduce the likelihood of encountering this issue.

Now onto the solution:

To address this issue, you should apply `position:fixed;` to the navbar instead of using `position: sticky`. The latter somehow alters the scrollTop calculation itself, causing the comparison to get stuck in a loop. Additionally, when using `position: fixed`, consider the offset of your content at scroll position 0, as it doesn't push the next child element down. You may need to add a margin top or something similar to address this.

I conducted a test using the Chrome inspector on your website. By changing 'header.is-fixed' to `position: fixed` and adding a 102px margin to the first section with the class 'hero is-medium has-text-centered-mobile has-bg-img has-text-centered', the bug was resolved.

I hope this information proves helpful in resolving the issue.

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 modal box should adjust its height to perfectly accommodate the content within

I'm in the process of developing a straightforward modal (popup) window, using as few lines of code as possible... just the basics. The modal has a maximum width of 650px and its height adjusts to fit the content. In case the content exceeds the avai ...

Step-by-step guide on displaying a window containing text when hovering over a CSS class

As a beginner in css, html, and js, I created an html page with a table of athletes. Each row is identified by the css class "athlete-name", like this: <div id="message-hide" style="display: none"> Hello world! </div> <t ...

Access to PHP script (IF) unattainable following a POST occurrence

I'm completely new at this. I am attempting to create a contact form using HTML5 and PHP mail function, but I am facing an issue with my form action pointing to contacto.php. After submitting the form, it seems to be skipping over the IF condition wi ...

Contrasting loading data from an empty state and having no items present

On my web page, I pull items from an API and display them. The API request is made in the created hook. I need to display a [loading] message while waiting for the API response, and a [no items] message if the loading is complete but there are no items ava ...

Is it possible to utilize a variable as a selector for the nth element?

Is there a way to select all elements with the class .formRow that have a value greater than a specified variable (x)? How can I reference this variable within the tag? $(document).ready(function(){ var x = 1; $('.formRow:nth('x') ~ .fo ...

Improving form handling with Vuex: Ensuring state is only updated after pressing the submit button

I am currently working on developing a form that pulls data from a Vuex store, allowing users to update the form fields and then submit when they are ready. Most tutorials I have seen use v-model with computed get() and set() to retrieve values from the s ...

What is the correct way to utilize ng-if/ng-show/ng-hide to hide or show HTML elements within the app.run function

I am currently working on developing an app that loads views correctly. HTML: <body> <loading outerWidth='1000' outerHeight='1000' display='isReady'></loading> <div class='wrapper' ng-sho ...

The margin data table unexpectedly grew on its own following the action of toggling column visibility

The margin table data mysteriously increased on its own after hiding / showing columns. Here is the original result before show/hide column: https://i.sstatic.net/N96HX.png After multiple show/hide actions (more than 10 times): https://i.sstatic.net/lPIG ...

What is the proper way to construct a URL with filter parameters in the RTK Query framework?

I am facing difficulty in constructing the URL to fetch filtered data. The backend REST API is developed using .Net. The format of the URL for filtering items is as follows: BASE_URL/ENDPOINT?Technologies=some-id&Complexities=0&Complexities=1& ...

Are there any solutions to refresh a page by clicking a button in next.js?

I am currently learning how to work with next.js and I'm facing an issue where I need to reload my page to make a new API request. As a beginner, I'm not sure how to achieve this. I've tried a few methods but none of them seem to work. Below ...

What is the correct way to implement the jQuery Highlight style?

At the bottom right corner of the jQuery Theme Roller website, you can find a preview of 'Highlight / Error' text. How can I effectively implement this feature in my own projects? I understand that I could simply copy and paste the surrounding c ...

Issue with retrieving relative time using Moment.js - fromNow()

I want to utilize moment.js to display relative time fromNow(), but I am encountering an issue where the values are being rounded and showing higher durations instead of exact completion times. For example, moment().subtract('s',110).fromNow() / ...

Having Trouble with the Dropdown Submenu in Bootstrap

Could someone assist me in getting this dropdown menu to function properly? I am trying to make it so that when the "Program" menu is clicked, a dropdown submenu will appear. I have inserted some code for it, but unfortunately it's not working. Just t ...

Dynamic links with Node Acl

Utilizing the npm module Acl to establish an ACL system has been my current focus. You can check out the homepage of this module at: https://github.com/OptimalBits/node_acl. While the documentation provides straightforward examples for granting role acces ...

Unable to access the done property in an AJAX JSON promise

Trying to dive into the world of JavaScript promises. My goal is to create a button that triggers a function displaying the result of a promise from another function. So far, I've been following tutorials and here's where I'm at: function my ...

configure various search parameters simultaneously during the rendering process

Incorporating a Tree component from mui v5, I am aiming to include searchParams for the selected and expanded nodes. This task is accomplished using the useSearchParams hook from React Router (v6). The issue arises when both the selected and expanded even ...

Utilize @Html.TextBox helper to include extra GET parameters

I have a webpage that includes a text input field, table, and pager. The text input is used to determine the number of rows displayed in the table per page. Here is the code for the view: @using (Html.BeginForm("Index", "Read", FormMethod.Get)) { ...

Angularjs scope throws TypeError when attempting to add an array

As someone diving into the world of Angular and JavaScript, I am tackling the challenge of creating a directive that requires adding an array to the directive's scope. Here's a snippet of the code for my directive: .directive("startupSections", ...

What causes the conflict between Nodejs usb module and Electron?

Apologies for the lengthy post, but I've been tirelessly searching for a solution online without any luck. My goal is to create a basic Electron application that can display an HTML page (which is working fine) and control a printer through the USB mo ...

Tips for concealing the "infoFiltered" feature in the jQuery datatable plugin

Within the datatable plugin, there is an option called "infoFiltered" which displays the total number of rows in the datatable across all pages. However, I am looking to hide this information. Is there a way to achieve that? ...