Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda