Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!