Ways to reveal a div once the user reaches the bottom of the page

http://codepen.io/BrianDGLS/pen/yNBrgR

This code snippet I am currently using helps the user keep track of their position on the page.

I'm interested in making a modification to this code: How can I make a div appear when the user reaches the bottom of the page, and stay visible until they refresh?

#show {display: none}

<div id="show">
<p>Hello</p>
<p>World!</p>
</div>

I want to display the div '#show' when the user scrolls to the bottom of the page, and have it remain visible as long as they are on that page.

Answer №1

By following a convention similar to the example JS code:

        $(window).scroll(function() {
          var wintop = $(window).scrollTop(),
            docheight = $(document).height(),
            winheight = $(window).height(),
            scrolled = (wintop / (docheight - winheight)) * 100;

          if (scrolled >= 100) {
            $(yourDiv).show();
          }
        });

The calculation of the scroll percentage is taken directly from the source you shared and the condition simply checks if you have reached 100% of the page (excluding the current window size).

You can also adjust the value from 100 to any desired percentage in order to display the div before the user reaches the very bottom.

Answer №2

Here's a possible solution:

$(window).on('scroll', function(){
    var bottomOfPage = $(window).scrollTop() + $(window).height();
    if( bottomOfPage >= $(document).height() ) $("#show").show();
});

Answer №3

In order to execute a Javascript function when the element with the ID of "show" becomes visible in the client's viewport, consider utilizing a plugin such as jquery.appear

Answer №4

Here is the code snippet to achieve the desired functionality:

let isVisible = false;
$(window).scroll(function() {
   if($(window).scrollTop() + $(window).height() == $(document).height() || !isVisible) {
       $("#show").show();
       isVisible = true;
   }
});

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 readStream in Node.JS unexpectedly terminates before the writeStream

I'm currently working on a project that involves fetching PDF files from remote servers and immediately sending them back to the clients who made the request. Here is the code snippet I am using: var writeStream = fs.createWriteStream(filename); writ ...

JQuery.each is causing an issue due to a potential conflict between JSON and string data types

Hey there, I'm trying to loop through each title in this code but encountering an error in the console that says "cannot use 'in' operator." Everything works fine when I provide an ID from the database. However, when I try to pass in a strin ...

Having trouble locating and interacting with the textarea element in Salesforce using Selenium Webdriver

Encountering an issue with the Selenium Webdriver where it throws an error stating that the element is not visible and cannot be interacted with when attempting to access a textarea. 1. The textarea is located within a pop-up window, which can be accessed ...

Securing CKEditor image upload URL with encryption

Once an image is uploaded in CKEditor, the full image URL is displayed. Is there a way to encrypt this URL within CKEditor? This would prevent anyone from accessing or viewing the original image URL. Thank you in advance. ...

What is the process for displaying a document file in an iframe that is returned from a link's action?

I have a main page called index.cshtml. This page displays a list of document files along with an iframe next to it. My goal is to load the selected document file into the iframe when I click on any item in the list. However, currently, when I click on a d ...

Deciphering base64 encoded data inside an HTML document and substituting these encoded strings with their decoded representation using Python

Can someone please assist me with this flipping program that is causing me endless headaches? I am dealing with multiple files containing base64 encoded strings. Here is an excerpt from one of the files: charset=utf-8;base64,I2J...encoded string... Thes ...

What is the best way to arrange an array of identical objects based on a specific sub property?

Here's an array of items to work with: myArray = [ { someDate: "2018-01-11T00:00:00", name: "John Smith", level: 5000 }, { someDate: "2017-12-18T00:00:00", name: "Jane Doe", level: 1000 }, { ...

How can we seamlessly transition from adding a class to setting scrollTop on a particular div?

I have successfully implemented a jQuery script to change the background color of my navbar on scroll. Here's the code I used: jQuery(document).ready(function($) { $(window).scroll(function() { var scrollPos = $(window).scrollTop(), nav ...

Rotating Images with CSS Transitions

Is there a way to rotate an image 90 degrees on click and then back to 0 degrees with another click, using only CSS? I found a solution with jQuery on this website, but I'm looking for a pure CSS alternative. ...

What could be causing the malfunction in my jQuery .each loop when trying to access the attribute of each element?

I am currently working on a script that is meant to extract the 'title' attribute from each child element within a form. It successfully retrieves the title when I use console.log('title'). However, I am facing an issue when trying to i ...

Slide up a div to reveal another div in its spot

I'm trying to incorporate a cool effect on my website where you hover over a box and it slides up to reveal more details in another div. I've seen this feature on websites like... http://www.facebook.com/pages/create.php In Facebook, the reve ...

Tool for tracking response time on basic Ajax-requested webpage

Looking for an easy way to use ajax to load content onto a page. I want the loaded content to wait before appearing on the page, but using jQuery's .delay() function didn't work as expected. Any suggestions on how to achieve this? Appreciate any ...

The perfect method for creating template literals using a function

I have a function that accepts an argument called id and returns a template literal: const generateTemplate = (id) => { return `<div style="box-sizing: border-box; height: 32px; border-bottom: 1px solid #ECECEC; color: #282828; padding: 8px; dis ...

Dealing with AJAX errors consistently in jQuery

Is it possible to efficiently handle 401 errors in AJAX calls and redirect to login.html without repeating the same code over and over again? if (xhr.status === 401) { location.assign('/login.html'); } I am seeking a way to manage these erro ...

Utilize generic types within methods in TypeScript

Here is a snippet of code I'm working on in TypeScript: public static sortByProperty<T>( array: T[], prop: string ): void { var availProps: string[] = Object.getOwnPropertyNames( T ); // or something typeof T, anyway I got error i ...

Tips for managing ajax responses using jquery

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <script> $(document).ready(function(){ $("#submit").on('click' ...

Retrieve Data from HTML Table using jQuery

I am working with an HTML table that has the following values: <tr class="tuker1"> <td class="tuker-lft1"> Username </td> <td class="tuker-rght1" onclick="awardRoute()"> <a href="#"> AWARD ROUTE </ ...

Why isn't the Bootstrap carousel showing up on my webpage?

I have been attempting to incorporate a bootstrap carousel into my project. However, upon inspecting the element, I can see the div and code, but no images, buttons, or any other content are visible. If anyone could take a look and offer assistance, it wo ...

Failed: Protractor could not synchronize with the page due to an error saying "angular is not present on the window"

I encountered an issue with my Protractor test scripts where I started receiving an error message. Everything was working smoothly until I made some updates to a few scripts in my projects. The error occurs at the end of running the scripts. I attempted ...

Having difficulty creating a responsive table with Bootstrap

I'm completely new to Bootstrap. Can you help me create a responsive table and make the body content responsive as well? <body style="background-color:#F1F3F6 "> <div align="center"> <h1 style="color:#0083CA;"> ...