Suppose you already have images encoded in your html document. What you need to do (if you want to do this with PHP), add a style attribute for each element. In your case, your images are stored in <a> tags, so you need to put the <a> tag randomly, not images ...
function generateRandomPositions(){ // define maximum and minimum 'top' values $maxTop = 1000; $minTop = 0; // define maximum and minimum 'left' values $maxLeft = 1000; $minLeft = 0; $styleProperties = array( 'position:absolute', 'top:'.rand($minTop,$maxTop) . 'px', 'left:'.rand($minLeft,$maxLeft) . 'px' ); return ' style="'.implode(';',$styleProperties).'" '; }
The function will return a string like this, -
style="position:absolute;top:10px; left:45px;"
Now all you have to do is call this function for every image on your page -
<a <?php echo generateRandomPositions();?> ><img src="... <a <?php echo generateRandomPositions();?> ><img src="... <a <?php echo generateRandomPositions();?> ><img src="... <a <?php echo generateRandomPositions();?> ><img src="...
The <a> tags will now contain random CSS positioning options, and their images will move with them.
As I'm sure, you can see that this method of using PHP to generate inline CSS properties is a kind of reverse way to do this. JavaScript is probably the best way to get what you are looking for, and there are other answers that cover it. First, you can hide your elements with CSS, and then display them with JavaScript after you set the random positioning.
Links -
source share