Bonjour à tous, j'ai une fonction qui me génère un nombre aléatoire pour modifier le background url de ma classe ".photo" en css, le problème est qu'a certain moment le même nombre aléatoire est généré et donc le même lien d'image est sélectionner, ce qui implique que la même image est affiché sur ma page. Comment puis-je régler cela ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 var images = []; var photo = document.querySelectorAll(".photo"); images[0] = "url(/images/test0.jpg)"; images[1] = "url(/images/test1.jpg)"; images[2] = "url(/images/test2.jpg)"; images[3] = "url(/images/test3.jpg)"; images[4] = "url(/images/test4.jpg)"; images[5] = "url(/images/test5.jpg)"; images[6] = "url(/images/test6.jpg)"; images[7] = "url(/images/test7.jpg)"; images[8] = "url(/images/test8.jpg)"; images[9] = "url(/images/test9.jpg)"; function random() { var random = Math.floor(Math.random() * 10); return random; } photo[1].style.backgroundImage = images[random()]; photo[2].style.backgroundImage = images[random()]; photo[3].style.backgroundImage = images[random()]; photo[4].style.backgroundImage = images[random()]; photo[5].style.backgroundImage = images[random()]; photo[6].style.backgroundImage = images[random()]; photo[7].style.backgroundImage = images[random()]; photo[8].style.backgroundImage = images[random()]; photo[9].style.backgroundImage = images[random()];![]()
Partager