JS图片随机显示

没什么难的,这里注意一下随机数的用法就行了

var ImgList=new Array;     //声明数组 
     
ImgList[0]="https://image1.jpg";
ImgList[1]="https://image2.jpg";    
ImgList[2]="https://image3.jpg";

var ImgChoice=Math.floor(Math.random() * ImgList.length);      //随机生成

document.getElementById('ImgContainer').css("background-image","url("+ImgList[ImgChoice]+")");   //设置图片
/*  $(".ImgContainer").css("background-image","url("+ImgList[ImgChoice]+")");  */

注:Math.floor(Math.random() * 10) 可以生成0~9的整数

原文地址:https://www.cnblogs.com/wqvincent/p/12856601.html