简体   繁体   English

带有 setInterval 的 javascript 随机图像背景

[英]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示例:我有一个图像选择作为imgg变量

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?现在我想通过那些数组颜色设置背景 .container 并想设置间隔,并且不停地改变,谁能给我一个建议?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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