Home > Article > Web Front-end > Use js to operate css to implement js to change the background image example_javascript skills
1. Use JS to define an image array, which stores the images you want to display randomly
2. Use JS to generate a random number. Of course, this random number starts from 0 and ends at imgArr.length-1
This way we get the current randomly generated image
3. Since a background image is randomly generated, use JS to use this image as the background image.
Since this is a demo, I defined a div with the ID BackgroundArea on the page, and also set a random background for this div.