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

167
Visualizações
How to change the position of images cloned with a script?

I want to clone an image and change the position of the cloned copy. I am cloning with the following script that follows an image defined in the document:

window.onload = function () {
    var div = document.getElementById('autumn1');
    var img = new Image(); 
    img.src = 'autumn-leaves-hd-png--3101.png';
    div.appendChild(img);

    img.height = window.innerHeight / 6;
    img.width = window.innerWidth / 6;

    function cloneImg() {
        div.appendChild(img.cloneNode(true));
    }

    for (let index = 0; index < 4; index++) {
        cloneImg();
        img.forEach(img.X = Math.random());
    }
}

How can I introduce changing of position of the cloned image, in the script above?

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

0

You can return the image element in the function.

Please see snippet below :

window.onload = function () {
    var div = document.getElementById('autumn1');
    var img = new Image(); 
    img.src = 'https://www.gravatar.com/avatar/bef07ce752c0e44449f4ec791d752871?s=48&d=identicon&r=PG&f=1';
    div.appendChild(img);

    img.height = window.innerHeight / 6;
    img.width = window.innerWidth / 6;

    function cloneImg() {
        return div.appendChild(img.cloneNode(true));
    }

    for (let index = 0; index < 4; index++) {
        let clone = cloneImg();
        let random = Math.random()*75;
        clone.style.position = 'absolute'
        clone.style.left =  random + "%";
    }
}
<div id="autumn1"></div>

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