What causes my sticky navbar's height to vanish when I scroll?

I've been working on a website that includes a sticky navigation bar created in JavaScript. The issue arises when I scroll - the navbar starts flickering and the body shifts up as shown in the images provided below. I'm puzzled by this problem.

Right after the navbar, there's a slideshow that gets cut off by the navbar resulting in me being at the top of the page.

Also, take a look at my code below...

https://i.sstatic.net/i4aFw.png https://i.sstatic.net/qiw7n.png

/*sticky_navbar*/
          window.onscroll = function() {
            myFunction()
          };

          var navbar = document.getElementById("header");
          var sticky = navbar.offsetTop;

          function myFunction() {
            if (window.pageYOffset >= sticky) {
              navbar.classList.add("sticky")
            } else {
              navbar.classList.remove("sticky");
            }
        }
@charset "UTF-8";
          
          /* CSS Document */
          
          body {
            margin: 0;
            font-size: 28px;
            background-color: #00011f;
            display: flex;
            flex-direction: column;
            margin: auto;
          }
          
          /* Style the navbar */
          
          #header {
            display: flex;
            justify-content: flex-end;
            background: rgba(139, 139, 157, 1);
            z-index: 2;
          }
          
          #Title {
            margin: 0 auto 0 0;
            height: 20px;
            margin-top: 20px;
            padding-left: 10px;
            border-bottom: 1px solid white;
            padding-top: 10px;
            padding-bottom: 35px;
            flex: 1;
          }
          
          #navbar {
            overflow: hidden;
            background: rgba(139, 139, 157, 0);
            display: flex;
            justify-content: flex-end;
            border-bottom: 5px solid white;
            padding-bottom: 20px;
            padding-top: 20px;
          }

          ... (content continues)

Answer №1

When the body jumps to another position, it may be because your navbar has a height of at least 86px, but you only provided .sticky+.content with a padding-top of 60px.

Consider using position: sticky; instead. Check out https://caniuse.com/#feat=css-sticky for more information.

Alternatively,

To prevent flickering, set the navbar's position to fixed on load:

#navbar {
  overflow: hidden;
  background: rgba(139, 139, 157, 0);
  display: flex;
  justify-content: flex-end;
  border-bottom: 5px solid white;
  padding-bottom: 20px;
  padding-top: 20px;
  position: fixed;
  top: 0;
  width: 100%;
}
.content {
  padding: 60px 16px 16px 16px;
  color: #ddd;
  background-color: #FFF
}

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

Submitting dataURL via Ajax using multipart/form-data

I'm currently working on a program that is responsible for extracting a dataURL from a canvas element and then transmitting it to the server side for conversion back into a JPG file. Now, my next step involves retrieving this image from the server pro ...

Exploring the use of jQuery or other JavaScript libraries for creating dynamic web applications

After completing a mid-level web application recently, I utilized Telerik controls along with native JavaScript functions in collaboration with other developers on the project. We encountered numerous cross-browser issues using this approach, although we ...

Deciphering the RGB depth data in three.js

Currently, I am working on implementing a basic GPU picker in three.js by using the MeshDepthMaterial. I have successfully extracted the color value by referring to this example code snippet: https://github.com/mrdoob/three.js/blob/master/examples/webgl_in ...

Is there a simple way to set the form inputs to right-to-left without excessive duplication?

Is there a way to apply the right-to-left (RTL) direction to a form without duplicating code for every input, based on the locale of the language being used? Can the locale be changed in CSS to make the form RTL more efficiently? For example: <div ...

Having trouble removing date ranges from input-daterange in the bootstrap datepicker

Utilizing the input-daterange feature of the Bootstrap datepicker has been effective for my project so far. However, I have encountered an issue with clearing the dates and selection range within it. $(document).ready(function() { datespicker(); }); ...

JavaScript and AJAX are showing an error message that says: "ReferenceError: x is not

I am currently working on a jQuery function that retrieves the value from a PHP-generated checkbox and sends it through AJAX. The value being sent is always a single word consisting only of letters. Here is the script I have written: <script type="text ...

Using the CSS property `absolute` can result in the input element shifting when entering text in Chrome

My grid layout is causing some issues with the search box placement. Whenever a user starts typing in the input box, it jumps to the other side of the page. After investigating, I realized that the culprit is the position: absolute property. Oddly enough, ...

Troubleshooting problems with ASP pages in WebMatrix 2

Every time I try to update my website and add new pages using WebMatrix 2, I encounter errors in my asp pages such as: "This document type is not supported. IntelliSense and validation for this document type will be based on HTML 5." The issue seems to b ...

Image floated to the left, text aligned in the center. Following the image, the links are not aligned

UPDATED: I recently made some adjustments to my webpage layout. There is an image at the top of the page that is floated left, and next to it, there is a list of links centered with text-align: center. However, the last link is centered within the div but ...

Managing the Loading Sequence of CSS Files in Nuxt and Vuetify

I am facing an issue in my Nuxt/Vuetify application where I cannot seem to load my custom CSS file after Vuetify's CSS. Despite attempting to change the order in the CSS array like so: css: [ '~/assets/style/main.scss', '~/asse ...

Determine the number of simultaneous key presses in JavaScript

Is there a way to accurately track the number of keys pressed simultaneously? I've made progress, but encountered issues when tabbing out of my browser window. If I press x amount of keys while tabbing away and then release them in another window, the ...

Enhance your photographic experience with the JavaScript Camera API on Android (Froyo

I have been attempting to utilize the JavaScript Camera API through the Android browser, as showcased at Google IO on Froyo. Despite having Froyo on my Nexus1, I am encountering difficulties accessing navigator.device and navigator.camera properties, bo ...

Error in THREE.js: Unable to access property 'lib' from an undefined source (THREE.ShaderUtils.lib["normal"])

I have been working on the challenges provided in the WebGL introductory book by Oreilly. Encountered a runtime error with the following code snippet. After searching online, it seems like I am the only one facing this issue. Could you please assist me in ...

What is the best way to create a form with two inputs that redirects me to a different page based on the inputs chosen?

Seeking assistance to resolve a challenging problem that I am currently facing. Any suggestions on the technology or backend skills needed for this task would be greatly appreciated, as well as any advice that can help me progress in the right direction. ...

gulp-open not functioning properly following the use of createWriteStream

I am utilizing gulp, gulp-eslint, and gulp-open to generate a report detailing ESLint outcomes. The linting and file creation processes function correctly; however, the task aimed at opening the file containing my report encounters issues. gulp.task(&apos ...

Conundrum encountered: SIGTRAP causing Electron failure to initialize

Exploring Electron for creating desktop applications has been my recent endeavor. However, I encountered this pesky error: /home/me/dev/my-electron-app-2/node_modules/electron/dist/electron exited with signal SIGTRAP Since the path leads to a binary file, ...

Numbering Tables Effectively in ReactJS

Currently working on coding a table in ReactJS and MUI, my goal is to number the No. column from 1 to the length of the array. This snippet shows my code: <TableBody> {quizes.map((quiz, index) => ( <TableRow key={quiz._id}> ...

a service that utilizes $http to communicate with controllers

My situation involves multiple controllers that rely on my custom service which uses $http. To tackle this issue, I implemented the following solution: .service('getDB', function($http){ return { fn: function(){ return $http({ ...

Utilizing Bootstrap 4 in ASP.NET MVC Framework

Greetings! Currently, I am working on implementing an ASP.NET web app using Bootstrap 4. However, since ASP.NET currently supports only Bootstrap 3, the templates are designed to fit Bootstrap 3. Upon upgrading, all the code logic changed and a new < na ...

"Capture live video using the reverse camera with the HTML5-QR Code

Recently, I started utilizing a JavaScript library called html5-qrcode (https://github.com/mebjas/html5-qrcode) for scanning QR Codes directly from my browser. The performance of this library is exceptional - it's fast and seamless! https://i.sstatic ...