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

229
Views
¿Cómo hacer filas y columnas de la misma imagen usando JS?

Me gustaría que una imagen apareciera x número de veces en filas y columnas. Entonces, si estoy creando un juego de multiplicación y la pregunta es "¿Cuánto es 9 x 5?", ¿Cómo me aseguro de que mi foto de 200x200 aparezca 45 veces en 9 columnas y 5 filas usando Vanilla JS? Me imagino que necesito hacer una tabla, pero no sé cómo hacerlo en función de cuáles son los dos números.

 let one = Math.floor(Math.random() * 9) + 1; let two = Math.floor(Math.random() * 9) + 1; let photos = document.getElementById('photos'); let product = one * two; let productNum = parseInt(product); const img = new Image(); const imgSrc = x.jpg; function generateImage() { const img = document.createElement('img') img.src = imgSrc; return img; } for (let i = 0; i < productNum; i++ ) { photos.appendChild(generateImage()); }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Podrías crear una función como esta:

 function createTable(w, h, src) { let table = document.createElement('table'); for(let i = 0; i < h; ++i) { let row = table.insertRow(); for(let j = 0; j < w; ++j) { let td = row.insertCell(); let img = document.createElement('img'); img.setAttribute('src', src); td.appendChild(img); } } document.getElementById('photos').appendChild(table); } createTable(9, 5, 'https://i.imgur.com/DfEE9nm.png');
 <html> <body> <div id='photos'></div> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

La forma más fácil probablemente sería crear un div para cada fila y agregar imágenes a cada fila para formar las columnas.

 let one = Math.floor(Math.random() * 9) + 1; let two = Math.floor(Math.random() * 9) + 1; let photos = document.getElementById("photos"); let product = one * two; let productNum = parseInt(product); const imgSrc = "https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico?v=ec617d715196"; function generateImage() { const img = document.createElement("img"); img.src = imgSrc; return img; } const rows = one; const columns = two; for (let i = 0; i < rows; i++) { const rowDiv = document.createElement("div"); for (let j = 0; j < columns; j++) { rowDiv.appendChild(generateImage()); } photos.appendChild(rowDiv); }
 <div id="photos"></div>

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!