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

219
Views
¿Cómo obtener una imagen aleatoria cuando recargamos el navegador HTML? usando javascript, css y html

Revisé las respuestas sobre este tema y probé un montón de ellas, pero todavía no me funciona. Me gustaría que apareciera una imagen diferente cuando recargo mi página HTML.

Por ahora, tengo este código que es bastante simple:

html:

 <head> <script type="text/javascript" src="../src/index.js"></script> </head> <body> <div id="container"> <img id="image_shower_vehicule" class="vehicule" /> </div> </body> </html>

CSS:

 #container{ position:relative; width:400px; height:400px; margin-left: auto; margin-right: auto; background-color: black; z-index:0; } .vehicule { position:absolute; z-index: 1; }

J.S.:

 var images_array_vehicule = ['vehicule1.png','vehicule2.png','vehicule3.png']; function getRandomImagevehicule() { random_index_vehicule = Math.floor(Math.random() * images_array_vehicule.length); selected_image_vehicule = images_array_vehicule[random_index_vehicule]; document.getElementById('image_shower_vehicule').src =`../public/layers/vehicule/${selected_image_vehicule}`; }

Tengo el siguiente directorio de carpetas...: https://i.stack.imgur.com/xtnML.png

¿Alguna idea que me pueda ayudar?

El paso futuro es hacer lo mismo con diferentes capas y superponer las imágenes elegidas con el índice z, pero por ahora solo quiero tener una imagen aleatoria cuando recargo la página.

Gracias por tu ayuda !

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Prueba esto :

función de llamada onload:

 <body onload="getRandomImagevehicule()">.... </body>

codigo js:

 var images_array_vehicule = ['vehicule1.png','vehicule2.png','vehicule3.png']; function getRandomImagevehicule() { random_index_vehicule = Math.floor(Math.random() * images_array_vehicule.length); selected_image_vehicule = images_array_vehicule[random_index_vehicule]; document.getElementById('image_shower_vehicule').src =`../public/layers/vehicule/${selected_image_vehicule}`; }
 #container { position:relative; width:400px; height:400px; margin-left: auto; margin-right: auto; background-color: black; z-index:0; } .vehicule { position:absolute; z-index: 1; }
 <body onload="getRandomImagevehicule()"> <div id="container"> <img id="image_shower_vehicule" class="vehicule" /> </div> </body>

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!