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

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

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