Why is the reverse sticky navbar not visible after the position changes from fixed to static?

Scenario

In the setup, I've implemented a reverse sticky navbar where it remains in position:fixed at the top. However, once the top of a specific div container touches the bottom of the navbar, I switch it to position:static using jQuery.

Challenge

The issue arises when the navbar transitions to position:static, causing it to disappear (you can see this problem clearly by viewing the code in full screen). What I actually want is for the navbar to remain fixed to the top of the div container as you scroll to the bottom of the page, eventually moving out of view when its position is no longer fixed. An example of the desired behavior can be seen here: http://jsfiddle.net/vx8uc2rm/.

var fixmeTop = $('.texttop').offset().top;
$(window).scroll(function() {
    var currentScroll = $(window).scrollTop()+210;
    if (currentScroll >= fixmeTop) {
          $('.header-container').css({
            position: 'relative'
            
        });
    } else {
         $('.header-container').css({
            position: 'fixed',
            top: 0,
            left: 0
            
        });
    }
});
body {
    background-color: #fbfaf8;
    overscroll-behavior: none;
    overflow: scroll;
}

.myheroimage {
    background-color:palevioletred;
    min-height: 1400px;
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    z-index: 0;
}

.header-container {
    width: 100%;
    position: fixed;
    z-index: 99;
    background-color: #fbfaf8;
    height: 180px;
}


header {
    display: flex;
    justify-content:space-between;
    padding: 30px 120px;
    align-items: center;
    margin-bottom: 80px;
}

a {
    color: white;
    text-decoration: none;
}

a.mybutton {
    color: #191919;
    text-decoration: none;
    font-family: 'Roboto', sans-serif;
    font-weight: 700;
    font-size: 1.5rem;
    letter-spacing: 3px;
}

a.mybutton:hover {
    color: #758086;
}

a.mybutton1 {
    color: #35BDAF;
    text-decoration: none;
    font-family: 'Roboto', sans-serif;
    font-weight: 700;
    font-size: 1.5rem;
    letter-spacing: 3px;
}

.mybutton{
    margin-left: 44px;
}

.texttop {
    text-align: center;
    font-family: 'Roboto', sans-serif;
    letter-spacing: 4px;
    font-size: 2rem;
    margin: 0 0 68px
}
.container-projects {
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    justify-content:flex-start;
    margin: 0 120px 120px;
}

.footer-container {
    background-color:#1E2224;
    color: white;
    font-family: 'Roboto', sans-serif;
    letter-spacing: 1px;
    font-weight: 300;
    font-size: 1rem;
    height: 150px;
    padding: 50px 120px 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home</title>
    <link rel="stylesheet" href="css-reset.css">
    <link rel="stylesheet" href="style.css">
    <link rel="preconnect" href="https://fonts.gstatic.com"> 
    <link href="https://fonts.googleapis.com/css2?family=Racing+Sans+One&family=Roboto:wght@300;400;700&display=swap" rel="stylesheet">
</head>
<body>
<div class="hiddenoverflow"></div>
<div class="myheroimage">
    <div class="header-container">
        <header>
            <div class="logo"></div>
      
        <nav>
        <a href="#" class="mybutton1">Title</a>
        <a href="#" class="mybutton">Title2</a>
        <a href="#" class="mybutton">Title3</a>
        </nav>
    
    
        </header>
    </div>
</div>

<div class="texttop">
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>

<div class="container-projects">

  <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia quam tempora quibusdam in atque! Nobis explicabo, at facere, sint sunt voluptas ad nesciunt deleniti dolorem aliquid dolor repellendus hic labore? [...]

Answer №1

If you are facing a problem, you may want to take a look at this fiddle:http://jsfiddle.net/dularnikode/742azj0x/5/ as it might provide a solution.

To address your initial issue, one approach is to apply the position value of "sticky".

body {
  height: 3000px;
}

.content {
  background: blue;
  height: 500px;
}

.fixme {
  background: green;
  color: white;
  text-align: center;
  width: 100%;
  position:sticky;
  top:0px;
}

.writehere {
  background: red;
  width: 100%;
}
<div class="fixme">Scroll here</div>
<div class="content"></div>
<div class="writehere">hi</div>

Answer №2

Found the solution! I implemented the scrollToFixed() jQuery plugin in my JavaScript file thanks to bigsotteddog https://github.com/bigspotteddog/ScrollToFixed#demos

$(document).ready(function() {
$('.header-container').scrollToFixed( { marginTop: 10, limit: 400 } );

The marginTop property determines where the navbar/header sticks, with 0 being at the top of the page, and the limit setting the scroll position at which it stops sticking.

This demo on the GitHub page really helped me grasp how to utilize the plugin, http://jsfiddle.net/y3qV5/435/.

I also found a similar question answered on Stack Overflow about a "Reverse" sticky header that provided some insight, "Reverse" sticky header.

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

Prevent financial disagreement by utilizing jQuery carefully

While I'm sure this question has been asked in a similar form before, my searches for the $ sign did not return any results here. I have already built a large system and heavily utilized jQuery, using $ as a reference. Now, I don't want to rever ...

How can I transform a JSON object into a series of nested form fields?

Can anyone suggest a reliable method for converting a JSON object to nested form fields? Let's consider the following JSON object: {'a':{'b':{'c':'1200'}}}, 'z':'foo', 'bar':{&apo ...

Is it possible to trigger an event just once?

Is there a way to ensure an event only fires once? I recently discovered that using .one seems to do the trick. ...

Improving efficiency for handling a vast number of inputs in React applications

Dealing specifically with Material UI, I am faced with the challenge of rendering a large number of inputs (more than 100) while effectively managing global state. Performance issues arise when using the Material UI <TextField /> component, with noti ...

What is the process for constructing an object to resemble another object?

After collecting input data, I have created an object based on those values. Here is an example of the generated object: var generate_fields = { name: "Mike", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4d9dddf ...

Utilizing AngularJS's ng-repeat directive in conjunction with a select input allows for seamless binding of an object

Upon receiving my get request $scope.variables = response.data; this is my select statement: <select name="varSelect" id="varSelect" ng-model="wid.adapter"> <option ng-repeat="variable in variables" value="{{variable.id}}">{{vari ...

Troubleshooting a position: fixed conflict in CSS or browser caused by a jQuery script

Check out this CodePen example. Go to the tab block by scrolling down. When you scroll to the top, it adds the 'sticked' class, and when it reaches the bottom of the parent element, it gets the 'sticked2' class. The problem arises when ...

Encountering a bindings issue when trying to utilize libxml-xsd within an electron application

Seeking guidance on validating an XML file against its schema within an electron application. Prior to adding the 'libxml-xsd' require statement to my angular2 service, it functioned properly. However, upon inclusion of this statement: const xs ...

Bootstrap's Vertical Margin Concept

Is there a way to include margin or a line similar to the image below? [![Please refer to the accompanying image][1]][1] ...

Reset hover effect upon exiting the current website

Hey there! I am currently dealing with an interesting situation regarding a link styled as a button on my website. <a href="http://externalsite" class="button" target="_blank">go to external site</a> Additionally, I have applied a hover effec ...

Check the status of the audio source URL using JavaScript

I am currently live streaming audio to my player using the Soundcloud API. <audio></aidio> <source src="soundcloud-track-url"></source> Within my code, I have added an onerror eventListener for both the <audio> and <sourc ...

The functionality of Angular material input fields is compromised when the css backface property is applied

I utilized this specific example to create a captivating 3D flip animation. <div class="scene scene--card"> <div class="card"> <div class="card__face card__face--front">front</div> <div class="card__face card__face--ba ...

Error message for invalid calling page when using the selenium + chrome.fileSystem.chooseEntry function

I am currently developing a Selenium script to automate testing of a Chrome app that relies on the Chrome.fileSystem.chooseEntry API for selecting a directory. Interestingly, manual selection using this API works perfectly fine. However, when I attempt to ...

Accessing dynamic elements in Internet Explorer 7 and 8 using jQuery

I have generated an HTML element dynamically using jQuery. Unfortunately, I am facing difficulties accessing the element utilizing jQuery selectors in IE7/IE8. For example: var id = 45; var $comment = $('#comment-'+id); // dynamically created ...

Transform the absence of value into an empty string within a function

I currently have a functioning code that I am pleased with and would prefer not to make any major changes, but rather just add a new feature that I require. However, I am unsure of how to go about it... Below is the existing code: const Obj = { "0": ...

The menu is loaded dynamically by Ajax from JavaScript once the page is refreshed

$('#subregionListPage').bind('pageinit', function(event){ var output = $('#subregions'); var region = getUrlVars()["reg"]; $.ajax({ url: 'http://localhost:8888/my/getsubregions.php', dat ...

Guide on adjusting shadow intensity with a Directional Light on a BoxGeometry in Three.js

I've been working on adjusting the light and shadow settings for Box Geometry. Specifically, I am using a Directional light with a certain intensity level. However, I've noticed that when I decrease the intensity, the plane appears darker while t ...

Chrome's inability to efficiently handle chunked responses in comparison to Firefox and Safari

I am currently working on a unique test node server that sends chunked responses every two seconds with the specified headers: response.setHeader('Content-Type', 'text/plain') response.setHeader('Transfer-Encoding', 'chu ...

What could be causing JavaScript fetch to only send OPTIONS requests instead of the expected calls?

I'm having an issue with my AJAX request using javascript fetch. It's only sending an OPTIONS call and not proceeding to make further calls. What's strange is that the response header seems fine, and $.ajax is working as expected. Check out ...

Tips for incorporating external libraries into a Grafana data source plugin

What's the best way to integrate an external library into a Grafana datasource plugin? My plugin is functioning properly, but I encounter an error when trying to use the "mqtt" library that I have installed and added to the package.json file: Plugin ...