Dynamic navigation bar that fades in and out while scrolling

My goal is to create a navigation bar that is initially hidden when the page loads, but appears when you scroll down to the second section. I have managed to make it work, however, there is an issue when scrolling up and down within the home section - the nav bar keeps flickering instead of staying hidden.

Check out the Live Demo:

Here's the JavaScript code:

<script type="text/javascript">
jQuery(document).ready(function() {
var startY = jQuery('#home').position().top + jQuery('#home').outerHeight();
jQuery('#nav-container').html( jQuery('#nav').html());
jQuery(window).scroll(function () {
    if(jQuery(this).scrollTop() > startY ){
        jQuery('#nav-container').slideDown();
    }else{
        $('#nav-container').css({display: 'block'});
        jQuery('#nav-container').slideUp();
    }
});
});
</script>

And here's the CSS style for the navigation container:

#nav-container {
    position: fixed;
    height: 50px;
    width: 100%;
    min-width: 600px;
    display: none;
}

I would appreciate any help or suggestions to fix this issue. Thank you in advance for your assistance.

By the way, this is my first time experimenting with JQuery and JS, so please be patient with me.

After making the necessary fixes, here is the updated version of the JavaScript code:

<script type="text/javascript">
$(document).ready(function() {
var startY = $('#home').position().top + $('#home').outerHeight();
var navc = $('#nav-container')
navc.html( $('#nav').html());
$(window).scroll(function () {
    if($(this).scrollTop() > startY ){
        navc.slideDown();
    }else{
        navc.slideUp();
    }
});
});
</script>

Answer №1

Due to the fact that you are currently within the .scroll() function, which triggers every time the page is scrolled, your code will constantly execute the else condition and display the navbar because of this specific line:

$('#nav-container').css({display: 'block'});

To resolve the issue, simply delete this line from your code.

Answer №2

To determine whether the navBar is visible, check its display property and only execute the scroll() function if it meets the desired condition. Here's an example code snippet:

    if(jQuery(this).scrollTop() > startY && $("#nav-container").css('display') == "none" ){
        jQuery('#nav-container').slideDown();
    }else if( && $("#nav-container").css('display') == "block"){
        $('#nav-container').css({display: 'block'});
        jQuery('#nav-container').slideUp();
    }

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

Is it feasible to insert the result of a JavaScript code into a PHP script?

Alternatively, you can view both codes side by side on the panelbackup(dot)com/codes page Alright, I have a code placed on page 1.html that adds four random numbers at the end of any given URL: <head><script>window.onload = function() { ...

Accessing website using Facebook credentials

I'm currently in the process of integrating Facebook Login on my website and have a few questions. I've encountered a problem where, upon receiving user permission, I need to create a new account in my database in order to utilize certain functio ...

Guide to integrating a fruit product checklist within a React application

Seeking assistance in incorporating a checklist into my react application, to only be visible after a specific button is clicked. Upon reviewing productFruit's documentation, it appears that I need to utilize the following code snippet: useEffect(() ...

Determine whether an element is currently focused using a Vue.js directive

I'm attempting to verify if an element is currently in focus, specifically an input field, and then apply a class to another element. This is the code I have been working on, but for some reason the hasFocus() function isn't functioning as expect ...

Execute a function upon clicking a checkbox in Backbone

I am seeking a solution on how to detect when a user clicks on a dynamically created checkbox. Once the checkbox is clicked, I want a specific message to be displayed in the div. However, if the checkbox is unclicked, another message should be shown, but o ...

Enhancing the aesthetic appeal of a form

I have created a form in HTML that utilizes JavaScript to pull data from a database. I am looking to style the form, but I'm unsure of how to proceed. Below is the form along with some CSS code. How can I integrate the two together? <form id=" ...

Exploring the functionality of Angular directives for bidirectional data binding with object references

In my custom Angular directive (v1.4.3), I am passing two bindings. The first binding is an Object with 2-way binding (model: '=') that can either be an Array or a single Object like [{something: 'foo'}, {something: 'moo'}, ...

What is the best method for invoking ajax requests from a service in AngularJS?

I am working on an Employee controller that includes properties such as Id, Name, and Specification. I have created an Employee service which makes an ajax call to retrieve a list of employees. However, every time I make the call, I receive an empty resp ...

Using React JS to send an object as an argument to an action function

How do I pass the "fields" object to the action "createPost" after creating it with validation in a form? Do I need to use "props" or could it be related to the dispatcher line? import React, { Component } from 'react'; import Header from ' ...

Utilizing BootStrap 3 to Display Dual Navigation Headers Side by Side in a Single Row

I need help with organizing my BootStrap 3 nav header. I want to move the single dropdown item to the far right, while keeping the rest of the main menu items aligned to the far left. Can someone guide me on how to achieve this? <nav class="navba ...

how to make a scroll event bubble in rxjs

When using vanilla JavaScript, I can easily catch any scroll event on any element by including true as the last argument: document.addEventListener('scroll', function(e) { console.log(e); }, true); However, with rxjs it's not as straight ...

Having trouble with implementing Nested routes in Vuejs?

main.js import Vue from "vue"; import App from "./App.vue"; import VueRouter from "vue-router"; import HelloWorld from "./components/HelloWorld"; import User from " ...

Moving a div with arrow keys using percentages: A step-by-step guide

I found this amazing script on Stack Overflow that allows me to move elements around the page using arrow keys. It works flawlessly, and I love how it enables diagonal movement by combining different arrow key inputs. Now, my query is whether it's fe ...

Using JQuery's addClass function inside another addClass function

When the h1 element is clicked, it should turn green. However, the h2 element does not turn red as expected. The syntax appears to be correct, so I'm unsure why this is happening. CSS: .red { color:red; } .green { color:green; } jQuery: ...

Retrieving an array from the $.get() method within multiple nested loops

I'm currently working on a jQuery plugin that takes an array of JSON files and needs to compile them into one large array. While each part of the code works individually, I'm facing issues when trying to integrate them together and return the ne ...

Build a home page in HTML with full width design

My webpage currently allows scrolling to the right and left in HTML. I would like to change this functionality. What I envision is having my cup and book visible without the need for scrolling, with a background image centered on the page. <div cla ...

What are the steps for running app.js deployed on a remote server using the local bash terminal?

After launching my web application on GoDaddy, which is built in Node.js, I have encountered a dilemma. In order to run app.js, I currently rely on my computer's bash command prompt. However, this poses an issue - if I were to shut down my laptop, the ...

When a button is clicked, the event is not triggered within a FirefoxOS application

Despite functioning perfectly on Firefox for desktop, my application encounters issues when tested on my ZTE Open C (Firefox OS 1.3). Pressing the button does not trigger any action, while onmouseup and onclick events work flawlessly. Even attempts to bin ...

The Ajax request is failing to communicate with the server

I am currently delving into the realm of Ajax and jQuery on a website I am working on. As someone new to Ajax, I have encountered a challenge. The webpage in question is a basic registration form where I aim to verify if the user's email address is al ...

Blank advertisements displayed on WordPress due to Google AdSense malfunction

My website on Wordpress can be found at . I encountered issues with the deprecated Adsense plugin for WordPress, so I created my own Adsense account. After verifying my site, I created a new ad unit and inserted its code into the body tag. The code for my ...