我想将Math.floor(Math.random())添加到图像中



我该如何使这行代码正常工作,以便它给我一个随机的图像,而不是有序的?我试过了,但那是不可能的。我如何在这里正确使用Math.floor-Math随机?

**block.style.content = `Math.floor((Math.random() * url(${nextImage()}))`;**      
var character = document.getElementById("character");
var block = document.getElementById("block");
var counter = 0;
var index = 0;
const images = [
'https://www.serebii.net/swordshield/pokemon/001.png',
'https://www.serebii.net/swordshield/pokemon/002.png',
'https://www.serebii.net/swordshield/pokemon/003.png',
'https://www.serebii.net/swordshield/pokemon/004.png',
'https://www.serebii.net/swordshield/pokemon/005.png',
'https://www.serebii.net/swordshield/pokemon/006.png',
'https://www.serebii.net/swordshield/pokemon/007.png',
'https://www.serebii.net/swordshield/pokemon/008.png',
'https://www.serebii.net/swordshield/pokemon/009.png'
];

function nextImage() {
index++;
if (index >= images.length) index = 0;
return images[index];
}
function jump() {
if (character.classList.contains("animate")) {
return
}
character.classList.add("animate");
setTimeout(function() {
character.classList.remove("animate");
block.style.content = `Math.floor((Math.random() * url(${nextImage()}))`;      
}, 300);
}
const images = [
'https://www.serebii.net/swordshield/pokemon/001.png',
'https://www.serebii.net/swordshield/pokemon/002.png',
'https://www.serebii.net/swordshield/pokemon/003.png',
'https://www.serebii.net/swordshield/pokemon/004.png',
'https://www.serebii.net/swordshield/pokemon/005.png',
'https://www.serebii.net/swordshield/pokemon/006.png',
'https://www.serebii.net/swordshield/pokemon/007.png',
'https://www.serebii.net/swordshield/pokemon/008.png',
'https://www.serebii.net/swordshield/pokemon/009.png'
];
**const nextImage = () => images[Math.floor(Math.random() * images.length)];** 

function nextFlyingImage() {
if (index >= flyingImages.length) index = 0;
return flyingImages[index];
}
function jump() {
if (character.classList.contains("animate")) {
return image[index]
}
character.classList.add("animate");
setTimeout(function() {
character.classList.remove("animate");
block.style.content = `url(${nextImage()})`;
//   flyingPokemon.style.content = `url(${nextFlyingImage()})`      
}, 300);
}

最新更新