Display a persistent bar on the page until the user reaches a specified <div> element through scrolling

Looking to implement a sticky bar at the bottom of my page that fades out once the user scrolls to a specific div and then fades back in when scrolling up and the div is out of view.

This bar should only appear if the user's screen size is not large enough to display the div normally.

Any suggestions or advice would be greatly appreciated!

I've tried using the following code snippet:

   <script type="text/javascript">
//<![CDATA[
$(window).bind("scroll", function() {
    if ($(this).scrollTop() > -1) { //Fade in at a level of height
        $("#bottbar").fadeIn();
        checkOffset();
    } else {
        $("#bottbar").stop().fadeOut();
    }
});
function checkOffset() {
    if($('#bottbar').offset().top + $('#bottbar').height() >= $('#stopDiv').offset().top) {
        $('#bottbar').css('position', 'absolute');
    }
    if($(window).scrollTop() + $(window).height() < $('#stopDiv').offset().top) {
        $('#bottbar').css('position', 'fixed'); // restore when you scroll up
   }
}
//]]>
</script>

However, it seems to be having some issues on my site bookaire.com. The bar doesn't show initially and gets stuck in the center of the screen instead of hiding when it reaches the stopdiv.

Answer №1

Finally! It took me some time to figure this out as I am still learning javascript. But I believe I have cracked it. Please note that the issue of the bar getting stuck in the middle on certain screen sizes is because you are only changing the position from fixed to absolute in your javascript. Instead, consider changing the display from block to hidden.
Also, ensure that the css for #bottbar is already set to display:block.

Based on my understanding, the requirements are:
1. It should not be visible if the user's screen size is large enough to show that div normally.
2. It should fade out when the user reaches a specific div by scrolling.

Here is the link to the code: JSFIDDLE

$(document).ready(function() {
    if($(window).height() > $('#stopDiv').offset().top) {
        $("#bottbar").css('display', 'none'); 
    }
    else {
        $("#bottbar").css('display', 'block');
    }
});
$(window).bind("scroll", function() {
    if($(window).height() > $('#stopDiv').offset().top) {
        $("#bottbar").css('display', 'none');
    }
    if ($(this).scrollTop() > -1) { //Fade in at a level of height
        $("#bottbar").css('display', 'block');
        checkOffset();
    } 
    else {
        $("#bottbar").css('display', 'none');
    }
});
function checkOffset() {
    if($('#bottbar').offset().top + $('#bottbar').height() >= $('#stopDiv').offset().top) {
        $('#bottbar').css('display', 'none');
    }
    if($(window).scrollTop() + $(window).height() < $('#stopDiv').offset().top) {
        $('#bottbar').css('display', 'block'); // restore when you scroll up
   }
}

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

The 'this' variable is malfunctioning when trying to assign a JSONP array to an AngularJS controller

I'm working with a REST service in my angular controller and using JSONP to make the call. I need to store the array that is returned from the service into a variable. This is what I currently have: this.testTheRest = function() { $http.jsonp(&a ...

Optimize HTML outputs in Smarty by reducing the file size

Is there a way to minify all output HTML templates in Smarty automatically? Would it be possible to set something like this: $smarty->minify = true; Additionally, I have come across the {strip} function but using it in every single .tpl file is not fe ...

Trigger the function upon displaying the modal

Within my Bootstrap project, I have set up a click event to trigger a modal as follows: $('#make_selects_modal').appendTo("body").modal('show'); My requirement is to run a function called pickClient when this modal is displayed. I att ...

Styling input[type='date'] for non-Chrome browsers with CSS

Looking for the css equivalent of: ::-webkit-datetime-edit ::-webkit-datetime-edit-fields-wrapper ::-webkit-datetime-edit-text ::-webkit-datetime-edit-month-field ::-webkit-datetime-edit-day-field ::-webkit-datetime-edit-year-field ::-webkit-inner-spin-bu ...

What is preventing me from installing react-router-transition on the 1.4.0 version?

$ npm install -S <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8dffe8eceef9a0ffe2f8f9e8ffa0f9ffece3fee4f9e4e2e3cdbca3b9a3bd">[email protected]</a> npm ERROR! code ERESOLVE npm ERROR! Unable to r ...

What is the best way to establish a maximum limit for a counter within an onclick event?

I'm struggling to figure out how to set a maximum limit for a counter in my onclick event. Can someone help me? What is the best way to add a max limit to the counter of an onclick event? Do I need to use an if statement? If yes, how should it be w ...

Looking to access a variable in Python Flask before it is referenced

I encountered an issue while trying to reference the variable "found_user," an object of the User class; it is defined within an if statement when a user logs in to the home page for the first time. My intention is to utilize the user's id to generate ...

Struggling to incorporate jquery and jquery-ujs into Browserify, encountering an error stating Uncaught ReferenceError: jQuery is not defined

I am currently working on a Ruby on Rails project and utilizing Browserify to manage my JavaScript dependencies. However, I keep encountering an error message stating "Uncaught ReferenceError: jQuery is not defined" because jquery-ujs is attempting to call ...

Issue with the intersection of mouse and camera rays in three.js

I've been working on a simple program that involves creating a clickable 3D object in Three.js. I've referenced my code from When I click directly on the object, it works as expected, but upon examining the resulting array, I noticed that the ob ...

Tips for Aligning h5 Headings with Unordered Lists

My footer contains the following code: HTML: <div class="container"> <div class="row"> <div class="col-md-6"> <div class="pull-right"> <h5><u><i>Information</i></u> ...

Steps for converting a JSON response into a .json file.Steps to transform a

I am looking to create a .json file within my local project directory. My goal is to store the response from a fetch API call, which is an array of objects, into a .json file. Here is the code snippet I am working with: ts : getRecords(){ this.serv ...

Node.JS Logic for Scraping and Extracting Time from Text

Currently, I am working on developing a web scraper to gather information about local events from various sites. One of my challenges is extracting event times as they are inputted in different formats by different sources. I'm seeking advice on how t ...

Tips for programmatically choosing images from a Google Photos collection?

Currently, I am attempting to use code in the console to automatically choose a photo from a Google Photos Album while browsing. I've tried this method so far: const photo = document.getElementsByClassName('p137Zd')[0].parentElement photo. ...

Is there a way to retrieve the JavaScript Console response code using Python and Selenium's execute_script method?

I am running a JavaScript fetch request in the Chrome developer console using Selenium's execute_script() function. The request performs as expected, but I want to set up a function that can verify if the response is a 403 status code. Is there any Se ...

What is causing the issue with using $("div[id*='\|']") in Internet Explorer 8 and 9?

section, have you encountered the issue where jQuery selectors fail to work with special characters escaped in IE8 and IE9? Presented below is the specific div that needs to be located: <div id="hello|12345"></div> The jQuery selector bein ...

Display the previous item I chose

When using jQuery to display an overview of user selections from a large section of select tags, I am facing an issue. Despite changing my selection multiple times, the code continues to show all previous selections along with the most recent one. This can ...

Obtaining NodeJS from a mysterious subdirectory

-- plugins ---- myplugin1 ------ core ---- myplugin2 ------ core If this represents the directory structure, is there a method to import all core directories from plugins without specifying the specific plugin names like myplugin1? require('/plugins ...

What is the method for including a TabIndex property in a JSON file?

https://i.sstatic.net/ISi72.png I discussed the integration of HTML fields within a JSON file and highlighted how to utilize the TabIndex property effectively in JSON files. ...

Updating Button Text with PHP on WooCommerce

Hi there, I'm trying to find a way to translate a button in my WooCommerce store without using LocoTranslate or any other tools. Is there a PHP function that can help me change the button text without needing an ID? If you want to take a look at the ...

What is a clever way to code this Angular HTML using just a single <a> tag?

<ul> <li ng-repeat="channel in Board.Channels"> <a ng-if="channel.key == ''" ng-href="/{{Board.type}}/{{Board.id}}/{{Board.key}}">{{channel.title}}</a> <a ng-if="channel.key != '&apo ...