im试图显示随机图像每1秒,它的图像在一个div内没有src属性。
<div class="D">
<img>
</div>
这里是css代码。
.D{
height: 30%;
width: 30%;
left: 35%;
top: 10%;
position: absolute;
}
.D img{
position: absolute;
height: 100%;
width: 100%;
}
I have an array that contains src of 4 diferrent images from the folder and with Math random im picking an random number from 0 to 3 as the array index and repeat the function with 间隔 :
var inp = document.querySelector('.D');
var p = document.querySelector('.D img');
var Src= ['Imgs/home.png','Imgs/gps.png','Imgs/bell.png','Imgs/box.png'];
var RndomFrom0To3 = Math.floor(Math.random()*4);
function ChangeImgSrc() {
p.setAttribute('src',Src[RndomFrom0To3]);
};
setInterval(ChangeImgSrc,1000);
The src changes but once and it stop in the same image what im i doing wrong ?
你也应该在每次调用中调用随机生成器
function ChangeImgSrc() {
var RndomFrom0To3 = Math.floor(Math.random()*4);
p.setAttribute('src',Src[RndomFrom0To3]);
};