Track the scrolling of <div> using pixel values

Is there a way to create a script that can show and hide a span based on the scrolling position of a div?

$("span").hide();
$(".box").scroll(function() {
  if (/* <div> scolling is euqal or less than 10px away from bottom: */){
    $("span").show();
  } else {
    $("span").hide();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="box" style="overflow: auto; height:100px">
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
  <p>a</p>
</div>
<span>Reached bottom</span>

Answer №1

To achieve the desired outcome, incorporate some offset API functionalities into your code.

If you are using jQuery, consider utilizing: $(something).offset()

Alternatively, in pure JavaScript (vanilla JS), you can use: window.pageYOffset alongside the scroll event

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

Implementing nested functions with a return value in JavaScript

As someone who is transitioning from a beginner to an intermediate programmer, I find myself struggling to grasp the concept of closures and nested functions with return values. Despite my efforts to read up on these topics, I still can't seem to full ...

Troubleshooting: Wordpress Ajax call failing with 500 Internal Server Error

Dealing with Ajax calls in a custom page template on Wordpress can be more complex than expected. I've struggled to get it running smoothly without crashing my entire site. It's puzzling why this particular approach is necessary when other Ajax c ...

Sharing information using Fancybox 2

I have encountered an issue while using a form within a fancybox window to post data via Ajax to a php page. The form works perfectly fine when run outside of the Fancybox - insertion and response are both successful. However, when I run the same page thr ...

Ways to set a default image for an <img> tag

I am looking to set a default image to the img tag in case the user has not selected a profile picture for their account. Here is the current output: http://jsfiddle.net/LvsYc/2973/ Check out the default image here: This is the script being used: funct ...

Exploring the power of JQuery's $.post() function and the magic

In order to utilize the GroupMe API (), the following command is required: $ curl -X POST -H "Content-Type: application/json" -d '{"source_guid": "frgfre", "text":"alala"}' https://api.groupme.com/v3/groups/ID/messages?token=YOUR_ACCESS_TOKEN I ...

Using double quotes when adding HTML5 local storage entries

Currently, I am setting the value of a field to a variable like this: newUser = document.getElementById('userName').value; Then putting that variable into local storage: localStorage.setItem('userName', JSON.stringify(newUser)); Fet ...

Steps for transforming a matplotlib animation into an HTML5 `<video>` element

Here is the code for a matplotlib animation graph and you can find instructions on how to save it here. from IPython.display import HTML import matplotlib.pyplot as plt import matplotlib.animation import numpy as np t = np.linspace(0,2*np.pi) x = np.sin ...

Animate an image to the right when clicked, then return it to the left with a second click

Seeking help with animating a set of images to move individually 300px right on first click, and then 300px left when clicked again. I'm currently facing an issue where my code is not working. It could be due to A) syntax errors or B) the images not ...

The technique of accessing parent props from a child composition component in React

I am trying to reduce every letter prop from the child component, Palata. How can I achieve this? index.js <Block letter="I" mb={16}> <Palata letter="I" start={4} end={9}/> <Wall/> <Empty/> <Palata le ...

Tips for minimizing react-redux boilerplate - I attempted implementing a ComponentFactory solution but encountered issues with react-redux validation errors

I had an idea to streamline the process in react-redux by creating a factory function called createScreen. Here's how it looks: ParentScreenFactory.js export default function createScreen(stateActions = []) { class ParentScreen extends React.Comp ...

Is there a more optimal approach to preserving color in point material within Three.js while avoiding the detrimental effects of additive blending mode?

Struggling to incorporate a map into a point material with a colored texture in Three.js. Attempted using additive blending and the map attribute to create a rounded point rather than a square one, but it results in the texture color blending with the whit ...

Mastering the art of CSS float and positioning techniques

Struggling with CSS positioning yet again. I'm trying to design a webpage with one main element in the center, surrounded by 10 others. The challenge is to maintain consistency across different screen sizes (excluding mobile). However, every time I r ...

Having trouble getting the Java applet to function properly on my HTML page

Currently, I am in the process of learning Java and came across an interesting concept that suggests I can also run my Java code in a browser using an applet. However, when I attempted to do so, an error message popped up prompting me to click for more det ...

The animation did not cause a transition to occur

After creating a search modal triggered by jQuery to add the class -open to the main parent div #search-box, I encountered an issue where the #search-box would fade in but the input did not transform as expected. I am currently investigating why this is ha ...

Issues with Gridview functionality for Javascript Calendar

The calendar feature in the Gridview is not functioning properly. I have assigned unique values to both the Gridview textbox and calendar image button. However, the click event of the calendar is not working within the Gridview. The same code works fine ...

How can an Angular Component be created in the browser using the standard method?

Attempting to develop a basic Angular example using JS/ESM. It has been some time since working within the angular environment, and there appear to be two primary choices: Utilizing the UMD lib (preferably to be avoided) Using the ESM2015 folder and loadi ...

Changing the position of the checkbox label to be above the checkbox

Can you help me with rearranging the checkbox label position to be above the checkbox instead of on the left side? I currently have the labels appearing on the left-hand side of the checkboxes, but I would like them to be positioned above the checkboxes. ...

Typescript is throwing an error when trying to use MUI-base componentType props within a custom component that is nested within another component

I need help customizing the InputUnstyled component from MUI-base. Everything works fine during runtime, but I am encountering a Typescript error when trying to access the maxLength attribute within componentProps for my custom input created with InputUnst ...

Differences in color rendering between XAML WPF and HTML

After migrating my app from WPF to HTML, CSS, and JavaScript, I noticed a discrepancy in color representation. Despite using the same hex value, the HTML color appears lighter than in WPF. See the comparison image here: https://i.sstatic.net/vB7rR.png In ...

What is the best way to populate the open areas on a grid using HTML?

I'm facing an issue with my note-taking app where, in a grid layout, short notes leave empty space. How can I push the cards below to move up and stick to the top card? I am currently using Bootstrap's grid layout for displaying the cards. You ca ...