Tips on keeping a div element in a fixed position until triggered by jQuery

I've managed to create a navigation bar within a header that sticks to the top of the screen once you scroll down past 100px. The functionality works well, but I'm looking to make the navigation bar stay fixed on the right until it snaps, essentially remaining in place until you scroll past 100px.

For a glimpse of my website, please check out .

Cheers!

Here's the relevant HTML:

<div class="header"> <!-- BEGINNING OF HEADER -->

    <div id="logo"> <!-- BEGINNING OF LOGO -->

    <h1 class="sliding-middle-out">Hi</h1>

    </div> <!-- END OF LOGO -->

    <div id="navigation">  <!-- BEGINNING OF NAVIGATION -->

    <div style="float:right"> <!-- BEGINNING OF IDK -->

    <div id="slider"></div>
    <a href="index.html"><div id="one" class="item"><div class="inside">Home</div></div></a>
    <a href="about.html"><div id="two" class="item"><div class="inside">About Us</div></div></a>
    <a href="content.html"><div id="three" class="item"><div class="inside">Content</div></div></a>
    <a href="contact.html"><div id="four" class="item"><div class="inside">Contact</div></div></a>

    </div> <!-- END OF IDK -->

    </div> <!-- END OF NAVIGATION -->

    </div> <!-- END OF HEADER -->

Here's the relevant CSS:

.header{
        height:100px;
        width:960px; 
        float:left;
        background-color:white;
      }
      #navigation{
        height:40px;
        font-size:20px;
        color:black;
        font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
        float:right;
        background-color:white;
        padding-top:5px;
        position:relative;
      }
      #navigation.stick{
        height:40px;
        font-size:20px;
        color:black;
        font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
        float:left;
        background-color:white;
        padding-top:5px;
        position:fixed;
      }
      .logo{
        height:100px;
        width:425px; 
        float:left;
      }

And here's the relevant JavaScript/ JQuery:

<script type="text/javascript" src="./jquery-2.1.4.min.js"></script>
    <script type="text/javascript" src="./jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="./jquery-1.11.0.min.js"></script>
    <script type="text/javascript" src="./jquery-latest.js"></script>

    <script>
$(document).on("scroll", function(){
    if($(document).scrollTop() > 1){
    $('#navigation').addClass("stick");
    }else{
    $('#navigation').removeClass("stick");
    }
});
</script>

Answer №1

If you want the stick class to take effect after scrolling 100px down the page, you will need to modify the JavaScript code as follows:

$(document).on("scroll", function(){
    $('#navigation').toggleClass('stick', $(document).scrollTop() > 100);
});

Answer №2

To achieve a fixed position for your header, include position:fixed in your header class.

I trust this information proves beneficial to you!

Answer №3

In order to lock the navigation bar in place, make adjustments to #navigation:

#navigation {
    ...
    position: fixed;
    right: 0;
}

Next, update the javascript with these changes:

$(document).on("scroll", function(){
    if($(document).scrollTop() >= $("#logo").height()-$("#navigation").height()){       
        $('#navigation').addClass("stick");
    }else{
        $('#navigation').removeClass("stick");
    }
});

This condition also ensures that the navigation bar stays within the header section, unless you prefer to keep your current code.

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

When using the jQuery ajax function to validate a form, it is important to note that the $_POST variables

I have an HTML form along with a jQuery function for form validation. After submitting the form values to my PHP files, I notice that the $_POST variable is not being set. What could be causing this issue? <form id="signup" class="dialog-form" action= ...

Press the Text and Alter Text with React

I'm having an issue with the onClick event using React hooks. My goal is to have the text change to a different one when the user clicks, and then revert back to the original text on another click. For example, clicking "Change First" should switch it ...

The ideal login page design

I apologize for my lack of knowledge in React, but I am quite new to it and have been struggling with this issue for days. I am having trouble creating a login page in ReactJS. Here is the code in my app.js: import React from 'react'; import {Ha ...

Unable to retrieve the width of an `element` using Angular's built-in jQuery method

I'm attempting to retrieve the width of an element using the AngularJS link method, but I'm not seeing the expected result. Here's my code: var myApp = angular.module("myApp", ['ngResource']); myApp.factory("server", function($r ...

Scroll positioning determines the height of an entity

Here's a code snippet I'm working with: HTML: <div id="wrap"> <div id="column"></div> </div> CSS: #wrap { display: block; height: 2000px; width: 400px } #column { display: block; height: 20px; ...

How can the HTML source code be printed without showing it in a window or tab?

Here's the scenario: I'm making an ajax post request to a controller action, which returns complete html source code. I want to print this html source as if it were being displayed in a browser without opening a new tab or window. Is there a way ...

Exploring how to replicate background-size: cover with a center position using HTML5's <video> tag

Looking to replicate the effect of background-size: cover on a <video> element. I was able to achieve this with the code below: <video id="videobackground" autoplay="true" loop="true"> <source src="BackgroundFinal2.mp4" type="video/mp4" ...

At what point does the event loop in node.js stop running?

Could you please enlighten me on the circumstances in which the event loop of node.js terminates? How does node.js determine that no more events will be triggered? For instance, how does it handle situations involving an HTTP client or a file reading app ...

Handling a WCF POST request with JQuery resulting in a 400 Bad Request HTTP Response

I'm encountering an issue with my JQuery POST not being accepted by the WCF Service. Below is the JavaScript code for the POST: function jqueryPost() { var url = "/LoggingTest"; $.post(url, { message: "test message" }); } To handle the POST ...

The load function is able to successfully load the page, however, it is unable to perform any actions with

When I use the following code: $('#categories').load("/Category/GetCats"); it calls the GetCats() method in this controller: public ActionResult GetCats() { var model = db.Cats .Where(c => c.user_id == 5); ...

A guide on enhancing Autocomplete Tag-it plugin with custom tags

Looking to expand the tags in the 'sampleTags' variable within this code snippet $(function () { var sampleTags = ['c++', 'java', 'php', 'coldfusion', 'javascript', 'asp', &apo ...

Enabling client-side access to collections without the need for meteor startup

Whenever I define my Meteor collections on the server and attempt to access them in the client without being within any of the predefined Meteor methods such as rendered, events, created, or helpers, I consistently encounter an error stating Meteor colle ...

Guide on dividing and presenting ajax information into two separate div containers

I am attempting to showcase two sets of data on separate divs using ajax. Below is the code I am utilizing for this task. Here is the ajax implementation $(function () { $(".myBtn").click(function () { var id = $(this).data("id"); ...

Creating a highly innovative server infrastructure tailored for Dojo applications with exceptional features

Recently, I have been using web2py for my projects and have found it incredibly useful in creating RESTful web applications. However, I am now looking to expand my skills in JavaScript by developing a more modern and dynamic client-side application that re ...

Message display showing an "[object Object]" (Node Express Passport)

Having an issue with my passport.js implementation where the flash message is not showing up when the username or password is incorrect. The logic was working fine before, but now it's broken even after copying the working version step by step. Flash ...

Attempting to transfer information from a JSON file to a Netflix-inspired platform

Images I am currently working on integrating data from my json file export const products = [ { name: 'iPhone 11', image: '/assets/images (7).jpeg', time: '1 hour 14mins', age: '+16&apo ...

Why am I encountering difficulties connecting to the Socket IO object in Node.js using Express?

Encountering a strange issue on the remote server side where everything works fine locally with a self-signed cert over https. However, when moving the code to the server, it works locally but not remotely. A node app is created and hosted on the server u ...

What is causing the alt or title tags to not function properly in emails?

I am currently facing an issue with e-mail templates that include images. The problem arises from the fact that these images need to be downloaded before they can be displayed in the email. To work around this, I always use ALT and TITLE tags to provide de ...

Updating a div element dynamically using AJAX in DJANGO

Currently in the process of developing a chat application. Using jquery $.post() to add chat messages has been successful thus far. My next step is to fetch the most recent chat message from the table and update the list on the chat page. As a beginner in ...

Having trouble with enter key input not triggering?

I have scoured various resources for answers to my query, including Stackoverflow. Unfortunately, none of the posts I came across have helped me resolve my issue with getting the enter key to work in my project for FreeCodeCamp on Codepen. When I press the ...