码迷,mamicode.com
首页 > 其他好文 > 详细

自动切换背景图片

时间:2018-10-20 16:13:29      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:body   math   class   刷新   inter   complete   com   code   https   

每次刷新页面自动切换背景图片

将以下代码放入body标签内,修改图片的URL地址即可

<script type="text/javascript">
    var img = Math.round(Math.random() * 10);
    var Img = new Array();
    //需要加载的背景图片
    Img[0] = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3735316392,212988712&fm=200&gp=0.jpg"
    Img[1] = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=4165415060,2023874983&fm=26&gp=0.jpg"
    Img[2] = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3706677817,1678627265&fm=26&gp=0.jpg"
    Img[3] = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=518541639,428272303&fm=26&gp=0.jpg"
    Img[4] = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1482498295,222129637&fm=26&gp=0.jpg"
    Img[5] = "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=357843698,3560590396&fm=26&gp=0.jpg"
    Img[6] = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1377601590,3543048053&fm=200&gp=0.jpg"
    Img[7] = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4189220961,2869949368&fm=26&gp=0.jpg"
    Img[8] = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3004924597,1854631352&fm=26&gp=0.jpg"
    Img[9] = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=299724880,3285215622&fm=26&gp=0.jpg"
    Img[10] = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2326892196,580815977&fm=26&gp=0.jpg"
    if(document.readyState === "loading" ||document.readyState === "interactive" ||document.readyState === "complete")
    {
        document.body.style.backgroundRepeat = "no-repeat";
        document.body.style.position = "center center" ;
        document.body.style.backgroundAttachment = "fixed";
        document.body.style.backgroundSize = "cover";
        document.body.style.backgroundImage = "URL("+Img[img]+")";
    }
</script>

觉得文章不错的,点个赞和关注吧

自动切换背景图片

标签:body   math   class   刷新   inter   complete   com   code   https   

原文地址:https://www.cnblogs.com/Lazy-Cat/p/9821578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!