Home  >  Article  >  Web Front-end  >  Play with js settings to randomly switch background images

Play with js settings to randomly switch background images

小云云
小云云Original
2017-12-22 10:01:081716browse

The function of js should not be underestimated. If you want to develop something well, js will greatly increase your score. This article will bring you a simple example of js setting to randomly switch background images. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look, I hope it can help everyone.

The examples are as follows:

<script>
 
var imgs =["http://images.cnblogs.com/cnblogs_com/xiaobo-Linux/1112236/o_2.jpg",
       "http://images.cnblogs.com/cnblogs_com/xiaobo-Linux/1112236/o_default.png",
       "http://images.cnblogs.com/cnblogs_com/xiaobo-Linux/1112236/o_3.jpg",
       "http://images.cnblogs.com/cnblogs_com/xiaobo-Linux/1112236/o_1.jpg",
       "http://images.cnblogs.com/cnblogs_com/xiaobo-Linux/1112236/o_4.jpg",
       "http://images.cnblogs.com/cnblogs_com/xiaobo-Linux/1112236/o_5.jpg",
       "http://images.cnblogs.com/cnblogs_com/xiaobo-Linux/1112236/o_6.jpg",
       "http://images.cnblogs.com/cnblogs_com/xiaobo-Linux/1112236/o_default-2.jpg",
       ];  
 
var index=Math.floor(Math.random()*8);
var img = imgs[index];
    function time(){
        
       document.body.style.backgroundImage="url("+img+")";
        
    }  
 
document.body.onload = function(){
 time();
}
</script>

The code looks very simple, I wonder if you have mastered it? Hurry up and give it a try.

Related recommendations:

JS method of randomly switching background images on web pages_javascript skills

CSS implementation of switching backgrounds by mouse passing through links Image example code_html/css_WEB-ITnose

IE6 a tag switching background image cannot take effect immediately_html/css_WEB-ITnose

The above is the detailed content of Play with js settings to randomly switch background images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn