Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
getBoundingClientRect() showing different values on load vs scroll

I'm working on a site for a client and trying to implement custom parallax functionality. I have used the following code -

var inView = function(element) {
    // get window height
    var windowHeight = window.innerHeight;
    // Get Element Height
    var elementHeight = element.clientHeight;
    // get number of pixels that the document is scrolled
    var scrollY = window.scrollY || window.pageYOffset;

    // get current scroll position (distance from the top of the page to the bottom of the current viewport)
    var scrollPosition = scrollY + windowHeight;

    var elementPosition = element.getBoundingClientRect().top + scrollY;

    var elementScrolled = elementPosition + element.clientHeight + windowHeight

    // is scroll position greater than element position? (is element in view?)
    if (scrollPosition > elementPosition && scrollPosition < elementScrolled) {
    return true;
    }
    return false;
}

// Get all the elements to be parallaxed
const parallaxElements = {
    element: document.querySelectorAll('#header-image img'),
    ratio: 0.25
}

// The parallax function
const parallax = elements => {
    let items       = [...elements.element],
        itemRatio   = elements.ratio

    if ('undefined' !== items && items.length > 0 ) {
        items.forEach( item => {
            if ( inView(item) == true ) {
                item.style.transform = 'translate3d(0, ' + (itemRatio * (window.innerHeight - item.getBoundingClientRect().top)) + 'px ,0)'
            }
        })
    }
}

//If element is in viewport, set its position
parallax(parallaxElements)

//Call the function on scroll
window.onscroll = () => {
    parallax(parallaxElements)
}

It's working ok except that when the page is loaded initially and the user starts scrolling, the position of element (#header-image img in this case) changes abruptly. I did some digging and noticed that the value of getBoundingClientRect().top is causing the issue.

When the page is loaded, it has some value, and as soon as the user starts scrolling, it abruptly changes to another value.

I am not able to figure out why this is happening. getBoundingClientRect().top is supposed to get the value of element from top of viewport, right?

Any help is greatly appreciated. Thanks!

Pls check the screenshot of inspect element here - https://i.stack.imgur.com/RYDvK.jpg

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda