The example in this article describes how to implement random switching of background images on web pages using js. Share it with everyone for your reference. The specific implementation method is as follows:
First of all, prepare some images. The size of the image (whether it is size or data size) must be controlled well. If it is too large, the user will not be able to wait to see the whole image and jump out. If it is too small, it will affect the page quality.
Organize these images into an array in script for easy calling. The length of the array is of course the number of images.
Because 5 images were used above, a random number from 0 to 4 needs to be generated here. If the array lengths are different, just modify the multiplier in the code below.
These are the core programs. Although it is very simple, it is a small idea. If based on this, some extended functions can be made through processing. For example: theme switching and other random presentations, etc. Below is the complete JS code.
I hope this article will be helpful to everyone’s web programming based on JavaScript.