Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
element.getBoundingClientRect is not a function

Why does it said that element.getBoundingClientRect is not a function? I really don't know what to do.

Here is my JS:

const elementsFadeIn = document.querySelectorAll(".fade-in");
window.addEventListener("scroll", fadeIn);
function fadeIn() {
  for (i in elementsFadeIn) {
    let element = elementsFadeIn[i];
    let viewElement = element.getBoundingClientRect().top - window.innerHeight + 20;
    if (viewElement < 0) {
        element.classList.add("view-element");
    } 
    else {
        element.classList.remove("view-element");
    }
  }
}

and my css code:

.fade-in {
   transform: translateY(50px) translateZ(0);
   transition-delay: 0.7s;
   transition: 1s;
   opacity: 0;
}
     
.view-element{
  opacity: 1;
  transform: translateY(0px) rotate(0deg) translateZ(0);
}
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You called element.getBoundingClientRect().top on a NodeList and not on the DOM element.

Solution: Access the element at index '0' of the NodeList, getting the DOM element. Remember you are using querySelectorAll so your code should be:

let viewElement = element[0].getBoundingClientRect().top 
about 4 years ago · Santiago Gelvez Report

0

The issue is that that the for ... in ... loop loops over all enumerable properties of an object. The NodeList that your querySelectorAll returns has numeric keys for each contained element BUT also contains the properties item, keys, values, entries, forEach, and length.

But these additional properties are not HTMLElements, hence, they don't have the method getBoundingClientRect.

See yourself:

const items = document.querySelectorAll('.item');

for (const item in items) {
  console.log('item: ', item);
}
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>

Instead you want to use the for ... of ... loop.

const items = document.querySelectorAll('.item');

for (const item of items) {
  console.log('item: ', item);
}
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>

about 4 years ago · Santiago Gelvez Report

0

Try this:

function fadeIn() {
    const elementsToFade = document.querySelectorAll(".fade-in");
    elementsToFade.forEach(element => {
        let viewElement = element.getBoundingClientRect().top - window.innerHeight + 20;
        if (viewElement < 0) {
            element.classList.add("view-element");
        } 
        else {
            element.classList.remove("view-element");
        }
    })
}
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!