Display Navigation Bar when Scrolling: Hidden upon Refresh

I'm currently using some jquery to create a fading effect on my Bootstrap 4 navbar when scrolling past a certain point. However, I've encountered a couple of issues. The navbar doesn't appear when reloading the page after already scrolled down, and when scrolling back up, the transition is abrupt rather than a smooth fade out. How can I address these problems? Would it be more effective to solely rely on jquery instead of utilizing a CSS class for this functionality? If so, how could that be implemented? Thank you!

JavaScript:


$(window).scroll(function(e) {
    if($(window).width() >= 768){
        var scroll = $(window).scrollTop();
        if (scroll >= 300) {
            $('.navbar-home').addClass("navbar-hide");
        } else {
            $('.navbar-home').removeClass("navbar-hide");
        }
    }
});

CSS:


.navbar-home {
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.5s ease-in-out;
    -moz-transition: opacity .2s ease-in-out;
    -o-transition: opacity .2s ease-in-out;
    -webkit-transition: opacity .2s ease-in-out;
}

.navbar-hide {
    opacity: 1;
    visibility: visible;
}

Answer №1

Make sure to invoke the identical code when the page is initially loaded. Modify the listener as follows:

$(window).on("scroll load", function(e) {...})

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 your CSS file functioning properly in Chrome but not in Internet Explorer?

I recently developed a custom SAP Portal Component that includes a JSP file with Javascript. This component is placed on a SAP Portal page alongside another iView, which contains multiple headers and text within an iframe. My objective is to dynamically ge ...

How to create a vertically scrollable div within another div by utilizing scrollTop in jQuery

I need assistance with scrolling the #container div inside the .bloc_1_medias div. The height of #container is greater than that of .bloc_1_medias. My goal is to implement a function where clicking on the "next" and "previous" text will scroll the #contai ...

Error encountered in jQuery call during Page_Load

Here is the code I am using to register a javascript function on Page_Load (I also tried it on Page_Init). This javascript function switches two panels from hidden to shown based on a parameter when the page loads: protected void Page_Load(object sen ...

import a file based on a specific condition in a Next.js application

Within my next.js + express.js (with a custom server within next) application, the following flow occurs: A user chooses a parameter from a dropdown menu. After selecting the parameter, a backend process is triggered. 2.1. Based on the selected parameter, ...

Guide on properly formatting arithmetic expressions in a TextView for TalkBack recognition

In search of a way to make a fitness app more accessible, I need assistance in displaying a message on a smartwatch emulator that can be easily understood by users utilizing the TalkBack feature. The challenge lies in ensuring that the word 'plus&apos ...

The CSS outline has a soft, rounded appearance rather than a sharp

https://i.sstatic.net/Mkb8Z.png Is there a way to prevent my outline from having rounded corners as it expands during the animation? Note: The issue seems to be present on Linux and MS Edge, but works fine on Mozilla Firefox. Any solutions for MS Edge us ...

Aligning input fields vertically and horizontally within a div container

Struggling with this frustrating piece of code for hours now. I've lost nearly 2 hours trying to get it to work properly. My goal is to center an input field vertically and horizontally inside a horizontal bar. Take a look at the simplified code belo ...

What is the best way to activate a button based on the presence of a cookie?

As I develop my web application, I am faced with the challenge of catering to both guests and registered users. I want to provide a 'Log out' button exclusively for registered users after they have logged in. One solution I am considering is to d ...

Encountering an Error while uploading to a NodeJS FTP server

I am utilizing the library https://github.com/mscdex/node-ftp to transfer a file to an FTP server. Below is the code snippet that I am using: const Client = require('ftp'); console.log('CONNECTING...') const c = new Client ...

altering the value with the click of a button

I'm new to JavaScript and I've come across a function that adjusts the quantity of ingredients on a recipe. I want to update the value by simply clicking a button. I have jquery included, the classes are properly formatted, and they are linked w ...

Transitioning Opacity in CSS

I found inspiration for my project from the following source: https://codepen.io/plavookac/pen/qomrMw One challenge I'm facing is changing the opacity of the movement, which is not covered by the standard transition markers. .mainInner{ display ...

Is there a way to adjust the position of ::before elements without affecting the border placement?

I'm struggling with an element that has a CSS style ::before to display an arrow-up inside a black circle. Check out the code here The issue I'm facing is that the character \25b2 isn't centered vertically within the circle. Adjustin ...

Dynamic Searching in ASP.NET Core MVC Select Component

I need assistance with implementing a dynamic search feature on my Login page. Here's the scenario: When a user enters a username, let's say "Jh" for Jhon, I want to display a select list next to the login form that lists all the usernames from t ...

Accessing variables from an external script in jsdom

Here is a simple example of jsdom code using the script parameter. Despite my best efforts to reference external JS files, I keep running into this issue: ReferenceError: exVar is not defined Does anyone know what might be causing this problem and how ...

How do I extract a parameter when passing it to a promise in NodeJS?

Let me simplify this query. I need to fetch a parameter used in a promise. Here's the gist of the code: function foo(param) { return fromPromise(blabla, blabla2, param) .then((res) => { return res }).catch((error) => { console.log( ...

It's next to impossible to secure expedited work on an ongoing project using Vercel

Yesterday, I successfully deployed an application on Vercel using only ReactJS. Today, I made the decision to develop an API for my application, To clarify, I have a folder housing the React app, and within that, I created a directory named "api" followi ...

I am facing an issue with my sequelize model where the "before create" functionality is not

Having issues with my before create hook in Sequelize model I've set up a template, but my before create hook doesn't seem to be working and I'm struggling to figure out why. async createUser(req,res){ const { name,email,login,password ...

Field for user input along with a pair of interactive buttons

I created a form with one input field and two buttons - one for checking in and the other for checking out using a code. However, when I submit the form, it leads to a blank page in the php file. What could be causing this issue? UPDATE After changing fro ...

What is the best way to add HTML content to a specific div element within an AJAX call's success function?

In the provided ajax script, my goal is to insert HTML content into a specific div element inside a table. The main div element in question is named "ajaxcom", but I need to insert the HTML content into a div element with the class = "divrep". How can I ac ...

When an Ajax post request is made, the data being sent is appended to the JSON response

Having a dilemma with my ajax call: $.ajax({ url: '/assets/functions.php', type: 'POST', data: { "functionCall": "get-uploads", "type": type }, dataType: 'json', success: function (data ...