The sticky navigation bar is not returning when scrolling up, and the function to remove the class is not working properly

After implementing a navigation bar that works on scroll down, I ran into an issue where it doesn't stop at the original position on scroll up. Despite finding a similar post addressing this problem, I struggled to apply the solution correctly due to my limited experience in web development. Initially, the navigation bar didn't work at all, but after digging through Stack Overflow, I managed to get it partially functional. Hopefully, someone can help me out with fixing it! Thank you in advance for taking the time to read and assist.

Relevant CSS-

nav {
    z-index: 500;
    background-color: #e7ecf2;
  }  
  .nav-placeholder {
    margin: 0 0 20px 0;
  }
  .fixed {
    position: fixed;
    top: 5px;
    left: 0;
    width: 100%;
    background-color: transparent;
  }
  .fixed .nav-inner {
    max-width: 700px;
    margin: 0 auto;
    background-color: transparent;
  }

Relevant JQuery-

$(document).ready(function() {
  var navOffset = $("nav").offset().top;
  $("nav").wrap('<div class="nav-placeholder"></div>');
  $(".nav-placeholder").height($("nav").outerHeight());
  $("nav").wrapInner('<div class="nav-inner"></div>');

  $(window).scroll(function() {
    var scrollPos = $(window).scrollTop();

    if (scrollPos >= navOffset) {
      $("nav").addClass("fixed");
    } else {
      $("nav").removeClass("fixed");
    }
  });
});

Relevant HTML-

<nav class="row wrapme container-fluid"><div class="nav-placeholder">
     <div class="nav-inner"><div class="col-sm-3">
  <button class="btn btn-block target">About Me</button></div>
       <div class="col-sm-3"> <button class="btn btn-block target" id="target2">Portfolio</button></div>
       <div class="col-sm-3"> <button class="btn btn-block target" id="target3">Contact</button></div>
          <div class="col-sm-3"><button class="btn btn-block target" id="target4">Links</button></div>
       </div> </div></nav>

Answer №1

I made a slight adjustment to the logic. If scrollPos is equal to navOffset, then the "fixed" class should be removed.

$(window).scroll(function() {
    var scrollPos=$(window).scrollTop();
    
    if (scrollPos > 0) {//changed removed = and navPosition parameter
   $("nav").addClass("fixed");
    } else {
      $("nav").removeClass("fixed");

    }
  });

Hopefully this clarifies things.

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

Revitalize JustGage using jQuery

I made a mistake while using jquery to refresh my JustGage gauges. The data from the controller is retrieved successfully, but unfortunately, I encountered an issue where the computer eventually runs out of memory and freezes. With 9 gauges and 2 pie chart ...

Using JQuery, retrieve an array of values for each distinct data attribute (which is dynamic) in the element

I am working with an HTML table that displays dates and values. The table is dynamically generated from a database upon loading, so the number of unique dates and corresponding "data-day" attribute values can vary. <table id="pricehistory"> <tr d ...

I am experiencing an issue with my Angular directive where the button click does not

Check out this page for guidance on adding a div with a button click in AngularJS: How to add div with a button click in angularJs I attempted to create an angular directive that should add a div to my HTML page when a button is clicked. However, upon cli ...

Ways to incorporate a reverse transition effect

I am encountering an issue with a sliding effect on a div. When it has a class hddn, the overlay will have right: -100% from the viewport. However, when the hddn class is removed, it should return to the viewport with right: 0. How can I animate this movem ...

Unable to retrieve scope variable within AJAX call

I am currently in the process of running a loop function that contains an AJAX request within it. However, I am facing difficulties with the success function on the AJAX not being able to access a counter variable outside of the loop function. var upda ...

Using PHP and Bootstrap to input data inside a modal

I am working on a page that has a form located here: https://jsfiddle.net/5tzg8kzm/9/ <body> <h1>&nbsp;</h1> <!-- Page Content --> <div class="container"> <!-- Trigger the modal with a button --> ...

JQuery failing to load in iOS UIWebView

I'm currently working on an iOS application with a webView, and I'm having trouble using JQuery. Despite copying the jquery.js file into my app, checking Compile Sources, and ensuring that the js file is listed under Copy Bundle Resources, it&apo ...

Learn how to send multiple callback functions in a jQuery ajax request

I have a JS file with multiple ajax calls and I am looking to make one AJAX call that triggers multiple callback functions. Can anyone assist me in achieving this? Below is an example of the test code: $.ajax({ url : url, dataType : 'json', ...

renewing the data in tag-it following the modification of the tag table

I currently have two scripts implemented on my MVC page. One script utilizes Tag-It, while the other allows me to add new tags through a dialog box. However, I am facing an issue where the new data is not loading when a user creates a new tag. I am looking ...

Use JavaScript to change the CSS pseudo-class from :hover to :active for touch devices

Looking to eliminate hover effects on touch devices? While it's recommended to use a hover class for hover effects, instead of the hover pseudo-class, for easier removal later on, it can be a challenge if your site is already coded. However, there&apo ...

How can I use jquery to eliminate classes that match?

I have a collection of items that are tagged with different classes: <ul> <li> <div class="blue active"><span>Blue</span></div> ...

Error in CSS styling affecting image display on responsive website

My CSS seems to have a bug when it comes to responsive pages with images. Take a look at the demo: https://jsfiddle.net/xr4getom/ If you resize the window on this example, you'll notice that a dark background (#222) appears intermittently. Is there ...

When hovering, apply style 1 to all elements with the same id, and style 2 to the hovered element

I'm working with some user-generated divs that I want to dynamically highlight when hovered over, while simultaneously blurring the other divs. My challenge is figuring out how to change the style of the hovered div separately from all the others. Th ...

Ensuring column content is perfectly aligned using Bootstrap 4

I'm attempting to use bootstrap to align rows and columns in my html so that it resembles the layout shown in this image.... https://i.sstatic.net/KC0sa.png I experimented with code like this... <div class="container"> <div class="row"> ...

Utilizing JQuery UI autocomplete for dynamically loaded textbox using AJAX

<div id='toolbox'> <div class='placeholder'></div> </div> In my project, I am using a click event to dynamically load a text box into the placeholder. $('#toolbox .placeholder').load('http:// ...

Exploring intricate toggle capabilities

Hello everyone, I am completely new to Jquery and recently stumbled upon this code snippet. It's an accordion that utilizes localstorage. $(function () { var initialCollapse = localStorage.collapse; if (initialCollapse) initialCollapse = in ...

Enhancing search functionality speed with a high volume of data

I am currently working on an application that functions as a search bar, sourcing data from a SQL table containing approximately 300,000 records. My goal is to incorporate an autocomplete feature into this search bar for enhanced user experience. I have e ...

Enhance DataTables functionality by including the ability to select which script to execute

Currently, I have a DataTables displayed with the provided code, utilizing server-side processing which is functioning properly. I am interested in implementing a dropdown menu above the table that allows users to select from options such as: Product Gr ...

Utilizing jQuery's getScript() method in combination with a specific div element identifier

I'm currently using jQuery to dynamically add a div to my webpage, which I plan to use as a container for content generated by another script (specifically, p5). Everything works smoothly when I manually include a static div with the id='p5canvas ...

What is the process of initializing divs in DataTables?

My application has a DataTable installed, but I encountered an error message stating "DataTables warning: Non-table node initialisation (DIV). For more details about this error, please visit http://datatables.net/tn/2". I'm aware that DataTables is d ...