Creating a div that becomes fixed at the top of the page after scrolling down a certain distance is a great way to improve user experience on a

I am struggling to create a fixed navigation bar that sticks to the top of the page after scrolling 500px, but without using position: fixed. Despite trying various solutions, none seem to work due to the unique layout of my navigation bar. Strangely enough, it works fine on jsfiddle but not on my actual website.

For reference, here is the jsfiddle link.

CSS

#background {
  background: lightblue;
  background-position:center top;
}

p {
  font-size:15px; 
  padding-top:100px;
  padding-left:100px;
  padding-right:100px;
}

.rotate {
  float: left;
 -webkit-transform: rotate(180deg) 2s;
  transform: rotate(180deg) 2s;
  transition: all 2s ease;
  transition-delay: 0.4s;
}
...
...
...

<p>HTML</p>

<pre><code>        <link href="https://fonts.googleapis.com/css?family=Dosis" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Exo:200|Open+Sans:300|Quicksand|Roboto+Condensed|Shadows+Into+Light" rel="stylesheet">

    (Html code continues...)

jQuery

jQuery(function($) {
  function fixDiv() {
    // jQuery code for fixing the navigation
  }
  $(window).scroll(fixDiv);
  fixDiv();
});

If anyone can offer assistance, it would be greatly appreciated! 😄

Answer â„–1

Assuming that your navigation bar is represented by the code <div class="dropdown">,

If you want to avoid using position fixed, you can use JavaScript to determine the top of your window as shown in this solution.

Then you can implement the following script to update the navbar's position based on the window scroll:

document.onscroll = () => {
  const navbar = document.getElementsByClassName('dropdown')[0];
  let top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);

  if(top > 500) {
    navbar.style.top = top;
  } else {
    navbar.style.top = 0;
  }
};

I haven't had a chance to test this yet, so it may require adjustments. I will provide an update once I have tested it.

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

Controlling the page scroll when adding data using jQuery

I am encountering an issue with a webpage that displays around 20 pictures in separate divs stacked vertically. Below these 20 pictures, there is a "show more" button that, when clicked, loads another set of 20 pictures and appends them to the existing dat ...

Refresh the Google Maps location using GPS coordinates

Currently, I am working with an Arduino that has a GPS chip and processing NMEA strings with Python. I have an HTML file set to auto-refresh every x seconds to update the marker's position. However, I would like to update the position information with ...

Internet Explorer 11 has a problem with excessive shrinking of flex items

I have a flexbox setup with a left and right section, each of equal width. The left side displays an image and the right side contains text. While this layout works fine in Google Chrome, it does not wrap properly in Internet Explorer 11 when the width is ...

Navigating through functions and saving outcomes

I need help creating a function that groups JSON elements based on a specific criteria, but I am having trouble with my loop. The goal is to create groups of 12 bottles and return a single JSON list. For example, in this case, the function should extract ...

Developing a Cloud Function for Stripe that is taking forever to finalize writing data to Firestore

Currently, I am facing an issue with my Google Cloud function (provided below) that handles webhooks from Stripe payments and stores some data in Firestore. The problem is that it seems to hang for approximately 3 minutes before completing. const Stripe = ...

Is there a way to stop a for-in loop within a nested forEach function in JavaScript?

I am facing a situation with nested loops for (var key in params) { if (Array.isArray(params[key])) { params[key].every(function(item) { let value = something(item.start, item.end); if (value === item.start || value == item.end) { ...

Sliding in the wrong direction on Bootstrap 5.2 carousel

I'm currently working on a carousel project. However, I encountered an issue where the images slide down initially and then jump up to the correct level as depicted in the image below. I've tried adjusting the height, width, and auto settings to ...

Selecting options in an input field using AngularJS

In my AngularJS template, I have the code snippet below: <input type="number" id="exercise-log-duration-hours-{{ ::$id }}" class="form-control" ng-model="$ctrl.workoutHours" ng-change="$ctrl.updateHours($ctrl.workoutHours)" name ...

Having trouble retrieving the table value from an HTML document?

I am trying to retrieve specific information from this source: This information is crucial for fetching data from a database using a primary key. However, extracting this value has proven to be quite challenging. Upon document readiness, I execute the fol ...

Effortless gliding towards the left

I am looking to incorporate buttons for smooth horizontal scrolling within my container. Currently, the functionality is in place but I would like to enhance its smoothness. How can I improve the scrolling experience? Should I consider using a different p ...

Achieving equal heights for div boxes while aligning list items inside

I have 8 green boxes on my site, each containing an image and either 3 or 4 list items with text only. The issue I'm facing is that the boxes are not of equal height due to two reasons: (1) One box may have an extra list item pushing the rest down, or ...

Troubles encountered when populating the array within a Vue component

I am experiencing an issue with my ProductCard component where the addItem method is not successfully adding a new item to the array. <template> <div class="card"> <div v-for="item in TodoItems" :key="item.id ...

Is it unnecessary to use both "width: inherit" and "float: none

When learning about responsive design, one pattern that is frequently seen is: @media screen and (max-width: 480px){ .class1{ width: inherit; float: none; } .class2{ width: inherit; float: none; } f ...

jQuery DataTables: varying column widths on page load versus after resizing

$('#some-table').DataTable({ "language": { "url": "{{ asset('/js/plugins/lang.json') }}" }, "ordering": false, "scrollX": true, "scrollCollapse": false, "searching": false, "processing": true, "serverSide": true, "scrollY": 400, "aj ...

Is it recommended for the browser to download multiple images when utilizing srcset?

Here is my HTML markup: <img srcset="https://unique.cloudinary.com/xxx/image/upload/c_scale,w_300/v1653408278/notforsquares/ll/DSCF4429.jpg 300w, https://unique.cloudinary.com/makingthings/image/upload/c_scale,w_600/v1653408278/notforsquares/ll ...

The Art of CSS Arrangement

I'm experiencing an issue on my website PSR Is there a way to ensure that the Footer always appears below the content div automatically? Both the Content and the Footer have relative positioning. Additionally, how can I make the Footer the same siz ...

Unique style sheet for unique content block

Recently I made some custom modifications to a block by adding style attributes directly into the .phtml file between tags. Now, I am looking to create a separate file for my block so that it can use a custom .css file. Where should I place this new file? ...

What is the best way to implement a day timer feature using JavaScript?

I am looking for a timer that can automatically change the rows in an HTML table every day. For example, if it is Day 11, 12, or 25 and the month is February at 8 AM, the rows should display "Hello!". function time() { var xdate = new Date(); var ...

A highly effective method for nesting items within a list through recursive functions

I am currently in the process of developing my own commenting system because I have found that existing platforms like Disqus do not meet my specific needs. My backend is built using NodeJS and MongoDB, and I need to execute two primary queries on my data ...

How can Jquery be used to target elements that have certain value(s) in their data attribute array?

I have the following elements: <div data-tags="[8,18,32,52,53,56]"></div> <div data-tags="[8,2,3]"></div> <div data-tags="[4,6,10]"></div> How can I select elements that contain "8" in thei ...