I have an HTML page with 5 images (48px × 48px each) on it. I want to appear the images at random locations on the webpage everytime the page is loaded.
I don’t know how to implement this, I just know I’m going to need CSS and JavaScript (for randomization) for this. Any ideas or suggesions?
Here is sample HTML code:
<a href="http://twitter.com/"><img alt="Twitter" src="/images/twitter-48.png" /></a>
<a href="http://facebook.com/><img alt="Facebook" src="/images/facebook-48.png" /></a>
<a href="https://plus.google.com/"><img alt="Google Plus" src="/images/plus-48.png" /></a>
<a href="https://github.com/"><img alt="GitHub" src="/images/github-48.png" /></a>
Lets assume that you already have the images hardcoded on to your html document. What you would need to do (if you want to accomplish this with PHP) is add a
styleattribute to each element. In your case, your images are held within<a>tags, so you’ll want to position the<a>tag randomly and not the images…The function will return a string similar to this –
Now all you have to do is call this function for each image on your page –
Your
<a>tags will now contain random CSS positioning parameters and their images will move with them.As I’m sure you can see, this method of using PHP to generate inline CSS properties is kind of a backwards way to do this. JavaScript would probably be the best way to get what you are looking for and there are other answers that cover it. You could initially hide your elements with CSS and then display them with the JavaScript after you have set the random positioning.
References –
rand()implode()