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

241
Visualizações
How to stop background image from scrolling any further past certain depth?

I'm trying to make background image follow scroll until it reaches certain depth (offSetHeight of a certain div), and past that I'm trying to make it stay in certain position. Here are some photos to explain it a bit better:

enter image description here

As we keep scrolling down, we can see background image going past div.

I've tried doing it in JavaScript, but to no avail as the picture just gets stretched and I can't manage to hit stop when bottom of background image hits upper part of div.

window.onscroll = function() {onScroll()};

function onScroll() {

    let scrollHeight = document.body.scrollTop;
    
    let element = document.getElementById("learnMorePt2IdStandard"); // div where image should stop
    let elementHeight = element.offsetHeight;
    console.log("Element height: " + elementHeight + "\nCurrent height: " + scrollHeight);

    if(scrollHeight <= elementHeight){
        document.body.style.backgroundAttachment = "";
    } else {
        document.body.style.backgroundAttachment = "local";
        document.body.style.backgroundSize  = "auto"
        document.body.style.backgroundColor = rgb(254,220,50); // cover all other parts of background that image won't cover (rgb function is defined and works as intended)
        // document.body.style.objectFit = "cover";
    }
}

I've tried trying different types of background-attachment properties as well as objectFit, but to no avail.

If problem is in background image I can always try to re-edit it but I think it could be done with JavaScript somehow using background-attachment property in CSS. Please let me know if I can explain it any further. Thank you!

EDIT: Here is where I'm trying to get background-image to stop. If user wants to continue scrolling past, picture should stay in place, and as user scrolls down, "the hand" would go above viewing area, and not follow scroll and "hide" behind gray div. enter image description here

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