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

112
Vistas
Images wont move based on changes with vanilla javascript

I am expecting the images to shift to the right. Runner increments and prints 1px, 2px, 3px etc. to console, but new margin wont be set. What's the problem?

Together with the code below, what I have written above should be sufficient to understand my problem. But I am, at this point, simply writing to get rid of the prompt to write more text.

<body>
    <div class="normal">
        <img id="normal" src="whiteboard.jpeg">
    </div>
    <div class="scaled">
        <img id="scaled" src="whiteboard.jpeg">
    </div>
</body>
<style>
    .normal{
        background-image: url('whiteboard.jpeg');
        position:absolute;
        z-index:-1;
    }
    .scaled{
        transform:scale(120%);
        z-index:2;
        clip-path: circle(5% at 33% 42%);
    }

    .normal, .scaled{
        width:100vw;
        
    }

    div img{
        width:100%;
        height:auto;
        
    }
</style>
<script>

    window.onload=function(){
        const normal = document.getElementById('normal');
        const scaled = document.getElementById('scaled');
        let runner =0;
        setInterval(function(){
            normal.style.marginRight="-"+runner+"px";
            scaled.style.marginRight="-"+runner+"px";
            runner++;
            console.log("respons - "+runner+"px")
        },50);
        
    }

</script>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The marginRight style describes the distance of the div element to its parent's right side. A negative marginRight will not work here - instead try marginLeft. Depending your desired direction of the animation use a positive or negative value.

window.onload = function() {
    const normal = document.getElementById('normal');
    const scaled = document.getElementById('scaled');
    let runner = 0;
    setInterval(function() {
        normal.style.marginLeft = "-" +runner + "px";
        scaled.style.marginLeft = "-" +runner + "px";
        runner++;
        console.log("respons - "+runner+"px")
    }, 50);
}
<body>
    <div class="normal">
        <img id="normal" src="whiteboard.jpeg">
    </div>
    <div class="scaled">
        <img id="scaled" src="whiteboard.jpeg">
    </div>
</body>
<style>
    .normal {
        background-image: url('whiteboard.jpeg');
        position: absolute;
        z-index: -1;
        background-color: blue;
    }
    .scaled{
        transform: scale(120%);
        z-index:2;
        clip-path: circle(5% at 33% 42%);
        background-color: red;
    }

    .normal, .scaled{
        width: 100vw;
        
    }

    div img {
        width: 100%;
        height: auto;
        
    }
</style>

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