Tips for Creating a Smooth Slide-Down Effect in Your Navbar Menu

Desired Outcome - My aim was to create a smooth slide down effect for the navbar after scrolling. While I managed to achieve this, I am encountering an issue.

The Issue - Whenever I scroll back up to the top, the navbar becomes sticky without any transition effect, which is affecting the overall aesthetic. Can anyone provide guidance on how to resolve this?

You can find my code on Codepen.

// Sticky Header
$(window).scroll(function() {
    if ($(window).scrollTop() > 100) {
        $('#header').addClass('sticky-header');
        $('#header .mo-row').removeClass('sticky-border');
    } else {
        $('#header').removeClass('sticky-header');
        $('#header .mo-row').addClass('sticky-border');
    }
});

var sidebarBox = document.querySelector('#box');
var sidebarBtn = document.querySelector('#btn');
var pageWrapper = document.querySelector('#main-content');

sidebarBtn.addEventListener('click', function(event) {

  if (this.classList.contains('active')) {
      this.classList.remove('active');
      sidebarBox.classList.remove('active');
  } else {
      this.classList.add('active');
      sidebarBox.classList.add('active');
  }
});

// accordion js

$(document).ready(function() {
    function close_accordion_section() {
        $('.accordion .accordion-section-title').removeClass('active2');
        $('.accordion .accordion-section-content').slideUp(300).removeClass('open');
    }

    $('.accordion-section-title').click(function(e) {
        // Grab current anchor value
        var currentAttrValue = $(this).attr('href');

        if($(e.target).is('.active2')) {
            close_accordion_section();
        }else {
            close_accordion_section();

            // Add active class to section title
            $(this).addClass('active2');

            // Open up the hidden content panel
            $('.accordion ' + currentAttrValue).slideDown(300).addClass('open');
        }
        e.preventDefault();
    });
});
<!-- CSS Code Here -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- HTML Code Here -->

Answer №1

After some modifications, the sticky header JS code now adds a class when scrolling up and shows another class once 200px have been scrolled from the top.

JS

 // Updated Sticky Header
 jQuery(window).scroll(function() {    
     var scroll = jQuery(window).scrollTop();
     if (scroll > 200) {
         jQuery("#header").addClass("show");
     } else {
         jQuery("#header").removeClass("show");
     }
 });

 var lastScrollTop = 0;
 jQuery(window).scroll(function(event){
 var st = jQuery(this).scrollTop();
    if (st > lastScrollTop){
        jQuery("#header").removeClass("up");
    } else {
       jQuery("#header").addClass("up");
    }
    lastScrollTop = st;
 });

CSS Changes Added

    #header-sec #header,
    #header-sec #header.up{
          position: absolute;
          top: 0;
          -webkit-transition: top 500ms ease-out ;
           -moz-transition: top 500ms ease-out ;
           -o-transition: top 500ms ease-out ;
           transition: top 500ms ease-out ;
    }
    #header-sec #header.show {
     position: fixed !important;
      top: 0;
      -webkit-animation-name: slidedown;
      animation-name: slidedown;
      -webkit-animation-duration: .5s;
      animation-duration: .5s;
      -webkit-animation-fill-mode: both;
      animation-fill-mode: both;
      -webkit-transform: translate3d(0, 0, 0);
      -moz-transform: translate3d(0, 0, 0);
      -ms-transform: translate3d(0, 0, 0);
      -o-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
    }
    #header-sec #header.up.show {
         position: fixed !important;
         top: 0;
	-webkit-animation-name: slideup;
	animation-name: slideup;
	-webkit-animation-duration: .5s;
	animation-duration: .5s;
	-webkit-animation-fill-mode: both;
	animation-fill-mode: both;
	-webkit-transform: translate3d(0, 0, 0);
	-moz-transform: translate3d(0, 0, 0);
	-ms-transform: translate3d(0, 0, 0);
	-o-transform: translate3d(0, 0, 0);
	transform: translate3d(0, 0, 0);
    }
    @-webkit-keyframes slideup {
      0% {
	opacity: 0;
	-webkit-transform: translateY(0);
	transform: translateY(0);
      }
      100% {
	-webkit-transform: translateY(-400px);
	transform: translateY(-400px);
      }
    }
    @keyframes slideup {
      0% {
	-webkit-transform: translateY(0);
	-ms-transform: translateY(0);
	transform: translateY(0);
     }
      100% {
	-webkit-transform: translateY(-400px);
	-ms-transform: translateY(-400px);
	transform: translateY(-400px);
      }
    }

Updated CSS for better functionality

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

Can you explain the purpose of the node_modules folder within AngularJS?

While exploring the AngularJS tutorial project, I came across the tutorial with a surprisingly hefty 60-megabyte node_modules directory. Does a simple clientside javascript project truly require such a large amount of unfamiliar data? I decided to delete ...

Center-aligning images in Bootstrap Carousel

I've implemented the Bootstrap 4 Carousel on my website and it's functioning properly. However, I'm facing an issue where I need to align the slider image in the center of the carousel. Currently, the image is aligned to the left side, caus ...

Some suggestions for updating two div elements using only one Ajax response

My website features a signin() button that I want to enhance with ajax functionality. When the button is clicked, I need it to update two divs: one displaying a personalized welcome message, and the other showcasing a statistics table in a different locati ...

PHP: When MySQL is not returning results and an undefined variable is causing issues

Currently, I am engaged in a project where I need to fetch some data from the database and display it within a form. Although my SQL query seems correct when reviewed in the SQL log, MySQL is not returning any results. I am seeking assistance on how to ret ...

Troubleshooting Problem with Web Forms and Bootstrap Form Design

Currently, I am utilizing a simplified version of the Admin LTE Bootstrap Theme in an ASP.NET Web Forms Project. In this project type, only one form tag can be used per page. The form tag must encapsulate both the Top (Nav) and Bottom (Side Bar Left & Mai ...

Steps for bundling a Node server with an Electron application

I am looking to package my Electron app, built with React.js, along with a local Node server into a single executable file. Is there a way to ensure that the separate Node server runs simultaneously with the Electron app when the program is executed? ...

Manage Blob data using Ajax request in spring MVC

My current project involves working with Blob data in spring MVC using jquery Ajax calls. Specifically, I am developing a banking application where I need to send an ajax request to retrieve all client details. However, the issue lies in dealing with the ...

What occurs when an npm module is removed from the repository?

I came across an interesting article discussing how the deletion of a popular npm package (left-pad) by its author led to the breaking of various apps. I am puzzled by this situation. Doesn't an npm package's code get locally downloaded when you ...

inserting various values from a web form

I have a form containing a table with three columns: Roll_No, Name, and Marks. The Roll_no and Name are fetched from the database, and the teacher only needs to enter the marks for each student. However, I am facing an issue where only the last entered val ...

Please disregard clicking on see-through areas of images

Currently working on a game project that involves layering multiple images (tiles) on top of each other to create a sense of depth. However, I have encountered an issue when attempting to click on these overlapping tiles. Due to their transparency, click ...

Exploring depths with recursive div search in JavaScript

Given a specific id of a div element on my webpage, I am looking to recursively search through all its children until I locate a div with a particular text string as its content, at which point I want to remove that element... function removeWhereTextIs(t ...

Tips for importing a JSON file from your local directory in Vue.js

I've been attempting to load data from a local JSON file in Vue. My goal is to simply load the data from the file and assign it to a variable. I'm not sure if I'm on the right track or if I'm missing something essential in my approach. ...

What is the best way to retrieve AJAX data in Django views?

I am currently facing difficulties with retrieving data from an AJAX call in my Django views using the GET method. Unfortunately, I am encountering issues accessing the data within my views and the console.log statements are not showing any output. As a ne ...

What is the best way to save the current time in a database using JavaScript in an ASP.NET application?

I positioned the Label outside the update panel so that any error or confirmation messages would be displayed on the top right side without reloading the entire page. However, the label is not displaying any text. When I comment out the update panel, it wo ...

Utilizing multiple instances of Express in Node.js

I've been struggling to grasp how the Expressjs framework for Node.js manages routes across multiple files. In my main index.js file, the setup looks something like this: var express = require('express'); var app = express(); app.use(' ...

Establishing the module exports for the NextJS configuration file

I have explored different solutions for adding multiple plugins to the next.js config file, with composition being the suggested method. However, I am encountering issues with this approach. const Dotenv = require('dotenv-webpack'); const withSt ...

Creating an external JavaScript and CSS file for date picker in Eclipse can be done by following a few simple steps. By creating separate files for the

I am using the following javascript and css styles: <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all" /> <script src="http://ajax.googleapis.com/ajax/libs/jq ...

What steps can I take to update the header and footer of my website?

Hey there! Just wanted to share that my website, newinternetdaily.com, recently got a fresh look with the installation of the newsmag cracked theme. However, I'm facing an issue with changing the default text in the header section since I don't k ...

Having trouble with a dropdown menu that allows for multi-select options?

var expanded = false; function showCheckboxes() { var checkboxes = document.getElementById("checkboxes"); if (!expanded) { checkboxes.style.display = "block"; expanded = true; } else { checkboxes.style.display = "none"; expanded = fa ...

Hide the scrollbar when scrolling is unnecessary

After applying the CSS below, a div I have now has a scrollbar. The issue is that even when scrolling isn't necessary, the bar still appears empty. Is there a method to only display the scrollbar if the content overflows? Any suggestions would be appr ...