Random background images CSS3

后端 未结 6 466
北荒
北荒 2020-12-10 07:36

I am doing small website, however I have like 5-6 images for my background, and I want to make it random every time I refresh the page. This is what I got in style.css :

6条回答
  •  轮回少年
    2020-12-10 08:06

    You cant use only html & css for this purpose. You should do it client side(like with javascript) or server side(like a php script)

    Here's php example:

    
    
    
    

    Here's jquery example:

    var images = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg'];
    $('html').css({'background-image': 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'});
    

提交回复
热议问题