What is the best way to determine if an HTML element reaches the bottom of the browser window?

In this scenario, my div is intersecting the bottom of the page. How can I use JavaScript to determine if this element is intersecting with it or not?

For example, if bottom:0px; should not be intersected.

.fixed{
  position:fixed;
  bottom:-10px;
  left:0px;
  width:300px;
  height:300px;
  background:blue;
  border:3px solid red;
}
<div class="fixed"></div>

Answer №1

To determine if an element intersects the screen, you can utilize the getBoundingClientRect() function which provides information on the size and position of the element in relation to the viewport.

By comparing this data to window.innerHeight, you can ascertain whether the element is within view or not.

var elmBottom = document.querySelector(".fixed").getBoundingClientRect().bottom;

if(elmBottom > window.innerHeight) console.log("The element intersects the bottom of the screen");
else console.log("The element is still fully visible");
.fixed{
  position:fixed;
  bottom:-10px;
  left:0px;
  width:300px;
  height:300px;
  background:blue;
  border:3px solid red;
}
<div class="fixed"></div>

Answer №2

After consulting a guide, I found code that determines if an element is within the viewport.

The script functions by comparing the element's position, obtained through getBoundingClientRect(), with the viewport's height calculated using documentElement.clientHeight.

In this scenario, to check only if the lower edge overlaps, adjustments were made to evaluate solely domRect.bottom.

This approach categorizes bottom: 0px; as overlapping and bottom: 1px; as non-overlapping. To classify bottom:0px; as overlapping, increment documentElement.clientHeight by 1.

function isIntersectingBottom(ele) {
    const domRect = ele.getBoundingClientRect();
    console.log(domRect.bottom);
    console.log(document.documentElement.clientHeight);
    return (
      domRect.bottom >= document.documentElement.clientHeight + 1
    );
  }
  
  const ele = document.querySelector('.fixed');
  console.log(isIntersectingBottom(ele));
.fixed{
  position: fixed;
  bottom: -10px;
  left: 0px;
  width: 300px;
  height: 300px;
  background: blue;
  border: 3px solid red;
}
<div class="fixed"></div>

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

What is the method for extracting CSS class names and storing them in an array using PHP?

Hey there, I have a bunch of CSS code and I'm looking for a way to extract only the names of the CSS classes without the unnecessary characters and values, and then store them in an array using PHP. For Example: .dungarees { content: "\ef ...

Automatically trigger the Submit button click with this selector

I'm having trouble automating the clicking of a 'Submit' button that only becomes clickable after a specific action is taken. In this case, the user needs to click the TOS checkbox before the button can be clicked. I've been unable to f ...

Advancement of a grunt chore within a digital platform

After constructing an app with grunt, I am now in the process of developing a web interface using node and angular to interact with this app. One feature I have implemented is a button that triggers a grunt task using childProcess in NodeJS: child_process ...

Easy method for generating a JavaScript dictionary using an array containing arrays

I have a collection of paired items, and I am looking to convert it into a dictionary. const collection = [['apple', 3], ['banana', 5]] const transformed = {'apple': 3, 'banana': 5} If I were using Python, I could ...

Angular Tip: Display data in a dropdown using a select element

When I have two select elements and want to display values if they exist: page.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app ...

Why isn't my onClick event functioning as expected?

I used the handleClick function in an onClick event, but it's showing an error (this is not defined). var Buttons = React.createClass({ getInitialState() { return { field: ':P ' } }, handleClick(field ...

Guide to showcasing associated information in HTML using Angular 7

I am a beginner with Angular 7 and I am attempting to showcase a product's color on the HTML side using Angular 7 but have not been successful. Below are my tables; Product Id Name Color Id Name ProductColorRelation Id ProductId ColorId In ...

How can you ensure your CSS code is functional across various browsers?

I have been exploring CSS and HTML for about a week now, but I am facing a challenge that has me stuck for the past thirty minutes. The issue lies in adapting this code to work smoothly across different browsers. While it aligns images in a row of four per ...

Javascript - Execute function after all nested forEach loops have finished running

I'm finding this task to be quite challenging, as I am struggling to comprehend it fully at the moment. The issue involves nested forEach loops, and I require a callback function to execute once all the loops have finished running. I am considering u ...

Submitting a file to the Slack API via the files.upload method using jQuery

I'm attempting to upload a file on a webpage and send it to Slack using the Slack API. Initially, my code looked like this: var request = require('request'); $(".submit").click(function(){ request.post({ url: 'https://slack.co ...

Add a border to the 'contenteditable' class in a Bootstrap table after it has been modified

Looking for help with JavaScript and jQuery! I have a script that creates an HTML table from a CSV file, using Bootstrap for styling. I want to add CSS or a border to a table cell after it has been edited, but my jQuery attempts haven't worked. Any su ...

Ways to protect Ajax link requests

Picture this scenario: a user is trying to register on a website and is filling out a form. As the user fills in the form, jQuery continuously validates fields using regular expressions, checks if they are valid, etc. The email address serves as the prima ...

The asp.net masterpage is not utilizing the stylesheet

I have the code snippet below in the MasterPage.master file of my project: <%= ConfigurationManager.AppSettings("System").ToString() %> <asp:ContentPlaceHolder ID="Stylesheets" runat="server"> <link rel="Stylesheet" href="/App_Dat ...

Submitting forms with CakePHP and AJAX

I'm struggling with implementing ajax and jQuery functions. I need to update my database table based on checkbox values. Below is the code snippet from a ctp file: $(".chk").click(function(e){ e.preventDefault(); var id = $(this ...

Sending information from a parent component to a nested child component in Vue.js

Currently, I am facing a challenge in passing data from a parent component all the way down to a child of the child component. I have tried using props to achieve this as discussed in this helpful thread Vue JS Pass Data From Parent To Child Of Child Of Ch ...

Executing pure JavaScript code in Grails using Groovy

this is a duplicate of Executing groovy statements in JavaScript sources in Grails with a slight variation, my intention is to only render the js-code without enclosing it in script tags picture someone loading a script from my server within their html l ...

Guide to fetching and returning data from AJAX using a function

In my JavaScript file, I have a function that retrieves a value asynchronously from the server: function retrieveValue(userId) { $.ajax({ type: "POST", url: "http://localhost/bghitn/web/app_dev.php/get_number_of_articles", data ...

Knowing how Meteor decides to recompute my template helper

Trying to grasp Meteor's reactivity has been quite the journey. The concept of it triggering a page re-render when a reactive data source in a template changes makes sense to me. I've also got a good handle on what qualifies as a reactive source ...

Encountered an issue with AWS S3: unable to retrieve the certificate from the local issuer

After completing my Protractor test suite execution, I am encountering an error while trying to upload my HTML result file to AWS S3 using JavaScript in my automation script. Can someone assist me in resolving this issue? static uploadtoS3() { con ...

Is the Bootstrap popover triggering the same action/event twice?

Have you ever encountered the issue of Bootstrap's popover repeating an action twice? It can be frustrating, especially when trying to submit a form inside the popover's data-content using AJAX. The form data gets repeated twice and the form is p ...