Problem with Jquery Sticky Navigation

I've been struggling to understand this issue and can't seem to find any help.

http://fiddle.jshell.net/DQgkE/7/show/

The user experience is currently a bit glitchy, but what I would like is:

1) When scrolling down the page, I want the Sticky Nav to be disabled at a specific location (chapter-3) on the page so that users can continue scrolling down.

2) When the user scrolls back up, the code should stick the nav back in place and move it up until it reaches its original position at the top.

Here's a starting point for reference:

3) It's kind of doing that already, but there seems to be a significant jump when scrolling back up.

Using options like disable, destroy, enable would be helpful.

Here's a cleaner version of the original experience: http://fiddle.jshell.net/DQgkE/1/show/

Thank you in advance for your help.

Answer №1

The functioning of the plugin you have utilized is not entirely clear to me, however, I have an alternative solution that I developed some time ago using jQuery. This solution includes a few variables at the beginning - the item you want to remain sticky, the point where you want it to stop moving, the class to apply when it becomes sticky, and padding at the top and bottom. In this version, I have only made modifications to the JavaScript section.

UPDATE I have gone ahead and rectified the original code. An alternate solution without the waypoint plugin has been provided in the comments.

Check out the result here: http://fiddle.jshell.net/Taks7/show/

Answer №2

My suggestion would be to consider incorporating jQuery into your project (surprise, surprise! :P)


$(document).ready(function() {        //when document is ready
  var topDist = $("nav").position();  //save the position of your navbar !Don't create that variable inside the scroll function!
  $(document).scroll(function () {    //every time users scrolls the page
    var scroll = $(this).scrollTop(); //get the distance of the current scroll from the top of the window
    if (scroll > topDist.top - *distance_nav_from_top*) { //user goes to the trigger position
      $('nav').css({position:"fixed", width: "100%", top:"*distance_nav_from_top*"}); //set the effect
      } else {                          //window is on top position, reaches trigger position from bottom-to-top scrolling
          $('nav').css({position:"static", width:"initial", top:"initial"}); //set them with the values you used before scrolling
      }
  });
});

I trust this information proves helpful!

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

Retrieve the current state of the toggle component by extracting its value from the HTML

I have a unique component that consists of a special switch and several other elements: <mat-slide-toggle (change)="toggle($event)" [checked]="false" attX="test"> ... </mat-slide-toggle> <p> ... </p> F ...

Tips for adding an additional div inside a navigation container, evenly spacing objects, and aligning the search bar to the right

I'm currently working on designing a simple navigation bar but I'm facing difficulties in aligning the elements correctly. See my progress here: https://jsfiddle.net/zigzag/bL1jxfax/ Here are my objectives: 1) Ensure that the navigation bar rea ...

Unable to update state from within a function in react

As I delve into the realm of coding, a simple graph catches my eye. It showcases various data sets including current, all, and filtered data. Initially, the first block of code runs smoothly. However, upon attempting to setState in order to make some modif ...

Valid ways to ensure that a CSS image expands outside the boundaries of the containing div

I have inserted an image using the following CSS code: .imgtemp { float:right; top:0px; left:0px; overflow:visible; width:100%; } Although I have added the div tag to display it on the page, ...

Obtain real-time information from an object using React

After developing an app using React, I encountered a scenario where I needed to work with data from an API. Here is the object structure: let currency = { "success": true, "timestamp": 1648656784, "base": "EUR", &quo ...

How can I generate codegen types using typeDefs?

I have been exploring the capabilities of graphql-codegen to automatically generate TypeScript types from my GraphQL type definitions. However, I encountered an issue where I received the following error message: Failed to load schema from code file " ...

What is the best way to retrieve comprehensive information from an API?

I have a task to complete - I need to retrieve data from the Pokemon API and display it on a website. This includes showing the name, HP, attack, defense stats of a Pokemon, as well as the Pokemon it evolves into. The challenge I'm facing is obtaining ...

What is the best method for aligning forms vertically within a page layout?

I'm working on designing a contact form, but I'm having trouble getting everything to align properly. Specifically, I can't seem to get the form elements to line up vertically and I also want the Message label to appear on the top left side ...

Header becomes distorted while scrolling down, then returns to normal when scrolling back up

Something strange is happening on my client's website. Whenever I scroll down and then back up, the header displays a large white area where the address/contact bar should be. You can see it in action on the site: rijschool-wolvega.nl I'm not w ...

What is the best way to conceal a div element on a Razor Page depending on the user's input?

How can I display the state field only when the user selects United States in the country field, and hide it otherwise? Here is the code snippet from my cshtml page: <div class="form-group col-sm-4 mt-4"> <label asp-for="Form.Co ...

Indicating a particular row with jQuery

I've created a snippet that functions correctly, but I'm looking to incorporate additional functionality and need guidance on how to do so. The current snippet effectively traverses each row, locates a specific cell, and performs some actions on ...

The background image fails to display on the button

Why isn't the background image for my button appearing when set using a CSS rule? I have a button with the class "todo" and although other parts of the rule work, the image itself does not show up even after trying "background: url" and "background-im ...

The function e.preventDefault() appears to be ineffective when applied to both the submit button and anchor tag within an

In my ASP.Net Core MVC App View <form> <div class="container"> <div class="row"> <div class="col-md-offset-2 col-md-4"> <div class="form-group"> <input type="text" class="form-contr ...

The process of integrating a Loader in Javascript

I am in need of a simple "page loading" animation while my photo is being uploaded. Unfortunately, when I tried using the "blockUI" JavaScript for this purpose, it didn't seem to work with my form. For uploading the image, I have employed a div as a ...

Implementing Material UI Slider component to update state upon mouse release, enabling real-time sliding functionality

Is there a way to update the new state only upon mouse release for a Material UI slider, while still allowing real-time tracking of the slide? Material UI offers two events: onChange and onChangeCommitted. The latter gives the desired end result, but the s ...

What is the correct way to utilize window.scrollY effectively in my code?

Is it possible to have a fixed header that only appears when the user scrolls up, instead of being fixed at the top by default? I've tried using the "fixed" property but it ends up blocking the white stick at the top. Adjusting the z-index doesn&apos ...

What is the best way to apply "display: none;" on a span element nested within a title attribute?

I am utilizing the social-share-button gem to share blog posts on social media. The website has been internationalized, making it bilingual (English and German). Everything is functioning correctly, but I encounter an issue with the social share buttons wh ...

"Enhancing web interactivity with AJAX requests and dynamic functionality in web

I'm finding it hard to understand the distinction between Rich Internet Applications and AJAX calls. From what I gather, any application that requires client-side execution can be classified as RIA. So, by this definition, should this website be cons ...

Refresh WebPage automatically after a Servlet successfully uploads and processes an image

I have a webpage that includes an image and a button. When the button is clicked, it uploads the image by submitting a form to a file upload servlet. However, after successfully uploading the image, the servlet does not display it in the img tag. Here is ...

Retrieve data from an HTML table with the power of jQuery

I have a table with the following structure. <form id="myform"> <table> <tr><th>TEST</th> <th>VALUES</th> <th>UNIT</th> <tr><td>Abc</td><td><input type="text" & ...