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

188
Visualizações
Can you set a max-background-size with JavaScript?

I'm using vanilla JS to increase a background-size value as the user scrolls down the page to create a parallax scroll effect. My function is below:

const heroBg = document.getElementById('hero');
window.addEventListener( 'scroll', function() {
    while (heroBg.style.backgroundSize <= '100') {
        heroBg.style.backgroundSize = 85 + window.pageYOffset/8 + '%';
        console.log(heroBg.style.backgroundSize);
        if (heroBg.style.backgroundSize === '100') {
            console.log('too big');
            break;
        }
    } 
});

The trouble I'm having is that the while loop only appears to run once and the image stops growing well before the 'backgroundSize' hits 100.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can't check for <= if using strings. Also, you can't add numbers and strings; you have to use brackets.

const heroBg = document.getElementById('hero');
window.addEventListener( 'scroll', function() {
    while (parseInt(heroBg.style.backgroundSize) <= 100) {
        heroBg.style.backgroundSize = (85 + window.pageYOffset/8) + '%';
        console.log(heroBg.style.backgroundSize);
        if (parseInt(heroBg.style.backgroundSize) == 100) {
            console.log('too big');
            break;
        }
    } 
});
about 4 years ago · Juan Pablo Isaza Relatório

0

the background size property has units of measurement, for example, px or %, but it is also a text property, and to perform a comparison operation, you need to first get rid of the units of measurement and then convert to a number. I suggest considering another option for performing the above task

    const $body = document.querySelector("body");
    let viewportHeight = document.documentElement.clientHeight

    const bgSize = _ => $body.style.backgroundSize = `${(document.documentElement.scrollTop + viewportHeight) / document.documentElement.offsetHeight * 100 * (viewportHeight / 100)}px`
    bgSize()
    window.addEventListener("resize", _ => {
      viewportHeight = document.documentElement.clientHeight
      bgSize()
    })
    document.addEventListener("scroll", _ => {
        bgSize()
    })
    * {
        padding: 0;
        margin: 0;
        box-sizing: border-box;
    }

    body {
        height: 300vh;
        background: radial-gradient(closest-side circle, black 0% 100%, transparent 100%);
        background-repeat: no-repeat;
        background-position: center;
        background-attachment: fixed;
    }

about 4 years ago · Juan Pablo Isaza Relatório
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