Is there a way to ensure that the navigation bar only appears at the top of the page?

Is there a way to only show the navigation bar when a user scrolls to the very top of the page? Currently, the nav bar appears whenever the user scrolls up, regardless of the screen position.

Html

<header class="nav-down">
 <div> navigation </div>
</header> 

Javascript

var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('#navigation-container').outerHeight();

$(window).scroll(function(event){
    didScroll = true;
});

setInterval(function() {
    if (didScroll) {
        hasScrolled();
        didScroll = false;
    }
}, 250);

function hasScrolled() {
var st = $(this).scrollTop();

// Ensure they scroll more than delta
if(Math.abs(lastScrollTop - st) <= delta)
    return;

// If they scrolled down and are past the navbar, add class .nav-up.
// This is necessary so you never see what is "behind" the navbar.
if (st > lastScrollTop && st > navbarHeight){
    // Scroll Down
    $('header').removeClass('navigation-container').addClass('nav-up');
} else {
    // Scroll Up
    if(st + $(window).height() < $(document).height()) {
        $('header').removeClass('nav-up').addClass('nav-down');
    }
}

lastScrollTop = st;
}

CSS

header {
height: 40px;
position: fixed;
top: 0;
transition: top 0.5s ease-in-out;
width: 100%;
z-index: 999;
padding-top: 50px;
}

.nav-up {
top: -70px;
}

Thank you!

Answer №1

To reach your goal, make sure to follow these two steps:

  1. Remove or comment out the entire navbar script.
  2. In the CSS, clean up the header declaration by keeping only the following 2 lines:
header {
  height: 50px;
  width: 80%;
}

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

Various settings in JQuery UI Slider

Does anyone know how to customize the steps in a jQuery UI slider? I want to set specific values as steps, like 0, 1200, 2000, 2200, and 3000. Any suggestions on how to achieve this? const rangeSliderInit = () => { const valueArray = [0, 400, 1000, 1 ...

Can you explain the concept of fallback color in CSS?

Can you explain to me what a fallback color means? I searched online and found out that it is used to display a color when the specified format is not supported by browsers, like Internet Explorer. Is there anything else I should know about fallback colors ...

Tips for extracting value from PHP and transferring it to a jQuery function

I am trying to calculate the distance between two airports and pass this value back to a jQuery function to use it in further calculations. I'm unsure of where I went wrong? HTML: The 'dept' and 'dest' inputs fetch airport informa ...

Ways to create a table with columns from various fields obtained through an API call

Looking to preprocess data received from an API, the raw data is structured as follows: https://i.sstatic.net/a9Q2Z.png Desiring to dynamically generate a table with columns based on the fields task_name and saved_answers. It's important to note tha ...

I am looking for a highly specialized jQuery selector that fits my exact requirements

Hello everyone, I'm encountering an issue with a jQuery selector. Here is the HTML code snippet: <div id="Id_Province_chzn"> <a href="javascript:void(0)" class="chzn-single chzn-default" tabindex="-1"> <span> + this.default_tex ...

"Exploring the world of jQuery and optimizing navigation for mobile

I am currently developing an HTML5 Mobile App where I have implemented panels as pages in the following way: <div class="upage panel" id="account_page" data-header="af-header-4" data-footer="none"> Additionally, I have created a login page structur ...

Which method is more appropriate for my request - GET or POST? Or should I consider

Recently, I've been exploring the world of get and post methods and could use some guidance! Within my App.js file, there is a user text input field and a submit button. I have a couple of tasks in mind for handling this information: Retrieve a str ...

Tips for showing form values in pop-up boxes based on their unique identifiers

I am experiencing an issue with displaying posted values in a pop-up box. Specifically, when I click on "Book now", it only shows one set of id values for all entries. For example, if I click on the 1st row's "Book now" button, it displays the values ...

Encounter a snag when attempting to upgrade to React version 16.10.2 while working with Ant Design Components - the error message reads: "

After upgrading to the latest React version 16.10.2, I encountered issues while using Ant Design Components. I specifically wanted to utilize the Title component from Typography. Here is an example of what I was trying to do: import { Typography } from & ...

The REACT- Popover feature seems to be having trouble showing the data from my json file

Within the menu/ section, the names of my invited guests are not visible; only the InfoIcon is displayed in the cell. My goal is to implement a Popover feature that will show all the information about the invited guests (including their names and locations ...

Leverage slot-specific information within the component's script in Vue

In my Vue project, I faced an issue where Component A has a slot that passes an object as slot-scoped to Component B: Template of Component A: <template> <div> <slot :myObject="myObject" /> </div> </template> Template of ...

Manipulating an HTML <div> element to display its value as a suffix in another <div> within the HTML code

I'm attempting to combine and show text along with content from another <div> as the modal-header, but it's not displaying correctly. When I give an ID to the modal-header, the *&times;* symbol disappears. This is how my modal-header l ...

Removing a dynamic component in Angular

Utilizing Angular dynamic components, I have successfully implemented a system to display toaster notifications through the creation of dynamic components. To achieve this, I have utilized the following: - ComponentFactoryResolve - EmbeddedViewRef - Ap ...

Submitting Angular 4 Form Reset Sends Data to Server

I am facing an issue with my HTML form: <form class="row" name="powerPlantSearchForm" (ngSubmit)="f.valid && searchPowerPlants()" #f="ngForm" novalidate> <div class="form-group col-xs-3" > <label for="powerPlan ...

The response from the Ajax call to the WCF is coming back as null

I am currently facing an issue where my ajax call to a function exposed by a WCF service is always returning 'undefined' in the success method, despite the fact that the function on the WCF side is returning the correct answer. I have debugged an ...

Select how the Android Keyboard functions on a mobile site

Currently running into a bit of trouble with my mobile website programming. It seems to be working fine on an iPhone browser, but when my coworker views it on an Android phone, the content gets all scrunched up when the keyboard is brought up. I've b ...

Prevent refreshing Google Maps when changing routes in AngularJS

Utilizing a Google map as the background for my website adds a unique visual element to the data displayed on different routes. However, I have encountered an issue where the map reloads with every route change, resulting in an unsightly flash of white tha ...

"Experience the power of Vue.js 3 and vue-router as the @click event seamlessly refreshes the entire

Just getting started with Vue and I'm trying to figure out how to toggle the menu open/close on mobile navigation without causing the whole page to reload. Any suggestions on how to prevent that? Here's my code: <router-link to="/ " @click="t ...

Passing HTML form variables to the controller in C# .NET Core

I'm struggling with passing a variable from an HTML form to a controller in my .NET Core project. The variable I pass to the controller is then used in a method call within an interface. Here's my data model for the form: public class JourneyFor ...

Setting up various connections is made possible through Node.js Socket.io

In the process of developing a straightforward chat application using socket.io and incorporating passport.js for user authentication, an issue arises when users log out and then back in. The previous socket connection remains active, resulting in two conn ...