Replace a css body background image style with code that randomly shows images on refreshing page?
This is the code that I want to replace
<style>
body{background: url('<?php echo $wo['config']['theme_url'];?>/img/welcome/background.png') !important;background-position: center !important;background-repeat: no-repeat !important;background-size: cover !important;}
</style>
I am new at this, I have tried code downloaded from the internet that worked using very simple html, javascript and css in a folder, but I can’t translate it to work with the above. I think it may not be relevant. That’s why I decided to post here.
At the moment the above code shows a single image for the background of the body tag. I would like the body background image to change to another image randomly choosen from images in a specfied folder. What would be the best way form me to acheive this?
3
Answers
First of all, please add code snippets around your HTML code and all your code globally. It’s more readable for users that want to help you.
Supposing your images are in the folder you provided, the code should be like this :
instead of
This will take all the files in your config directory, and select an index randomly. Then you have just to pick the file name of the index
$file
and display it withecho
.You can try to generate a random number that changes the name of the file that acts as wallpaper
With this code it will try to access 10 different images called:
background1.png,background2.png ..etc
For this you must have those 10 images with those names in your images folder
to randomly background image you can write some JavaScript like this: