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

110
Visualizações
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 Respostas
Responde à pergunta

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 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