简体   繁体   中英

random image background with javascript with setInterval

How to get a random image background for our webpage by Set interval?

Example: I have an image choice as imgg variable

let imgg = [ "red", "pink", "yellow", "brown", "white"]

let foto = document.querySelector(".container")

Now I want to set that background .container by those array color and want to setInterval, and keep changing without stop, can anyone give me a suggestion?

Use from this code:

 var images=['http://www.planwallpaper.com/static/images/stunning-images-of-the-space.jpg', 'https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/pia20645_main.jpg?itok=dLn7SngD', 'http://24space.ru/uploads/posts/2014-12/1418220047_asteroid.jpg', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT-1jIpSolSoVubMTdKYhIJyQxqEOK66TT01y2PJXrddMsPz2dx']; var url=0; setInterval(function(){ url+=1; if(url==4){ url=0; } document.body.style.backgroundImage = 'url('+images[url]+')'; document.body.style.backgroundRepeat = "no-repeat"; },5000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM