Connecting Tech Pros Worldwide Help | Site Map

Placing something in a random location on a page

Newbie
 
Join Date: Mar 2007
Posts: 17
#1: Apr 19 '07
Ok, I'm trying to make something appear in a random location on a page. I have an idea how to do it, make a random variable to represent its pixel hieght and width on a page, but I'm not sure how to assign a pixel height or width for a picture. Help plz?
(by the way I may be completely wrong about this)

THANKS!
Newbie
 
Join Date: Mar 2007
Posts: 17
#2: Apr 19 '07

re: Placing something in a random location on a page


Is there any way to make a picture apear on a random location on a page? I was thinking 2 random variables to represent the x and y location, but I dont know if thats possible, and if it is, I have no idea how to do it.

THANKS FOR YOUR HELP!
Familiar Sight
 
Join Date: Feb 2007
Posts: 135
#3: Apr 20 '07

re: Placing something in a random location on a page


Hi,

I think u should go for DHTML effects that uses css. To be more specific u can use div and put the image within the div. Now using style tag for the div u can mention the height, width, positions of the div within the page. U can change the positions randomly using script to display the image at random places within the page.

susen
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#4: Apr 20 '07

re: Placing something in a random location on a page


I wouldn't worry about size and position-
just use insertBefore a random element of the document.


Expand|Select|Wrap|Line Numbers
  1. function randomPlace(what){
  2.     tag='*', pa= document.body;
  3.     if(what.nodeType!=1) return false;
  4.     var A= pa.getElementsByTagName(tag);
  5.     var who;
  6.     while(!who) who=A[Math.floor(Math.random()*A.length)] ;
  7.     try{
  8.         who.parentNode.insertBefore(what,who);
  9.     }
  10.     catch(er){
  11.         who=false;
  12.     }
  13.     if(!who) arguments.callee(what);
  14. }
var what=document.images[0];// reference some element
randomPlace(what)
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#5: Apr 20 '07

re: Placing something in a random location on a page


If you are careful or careless you can skip the error handling:
Expand|Select|Wrap|Line Numbers
  1. function randomPlace(what){    
  2.     var A= document.body.getElementsByTagName('*');
  3.     var who;
  4.     while(!who) who=A[Math.floor(Math.random()*A.length)] ;
  5.     who.parentNode.insertBefore(what,who);    
  6. }
Newbie
 
Join Date: Mar 2007
Posts: 17
#6: Apr 26 '07

re: Placing something in a random location on a page


Does anyone know how to place a picture on a random location on a page? I know how to specify where it goes but do you know how to make that random? Could you combine javascript and CSS to make a random number for the pixel location?

(P.S. If you can combine javascript and CSS plz tell it would help me a ton!)

THANKS!
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#7: Apr 27 '07

re: Placing something in a random location on a page


sample code i wrote up for ya

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function RandomPlaceImg(img) {
  3.  var img_holder = document.createElement("<div id=\"img_holder\" name=\"img_holder\">");
  4.  img_holder.innerHTML = "<img src=\""+img+"\" id=\"theimage\" border=0>";
  5.  img_holder.style.position = "absolute";
  6.  document.body.appendChild(img_holder);
  7.  var grab_image = document.getElementById("theimage");
  8.  var x = document.body.offsetHeight-grab_image.height-5;
  9.  var y = document.body.offsetWidth-grab_image.width-5;
  10.  var randomx = Math.floor(Math.random()*x+1);
  11.  var randomy = Math.floor(Math.random()*y+1);
  12.  img_holder.style.top = randomx + 'px';
  13.  img_holder.style.left = randomy + 'px';
  14.  }
  15. </script>
  16. <body onload="RandomPlaceImg('http://images.google.com/intl/en_ALL/images/images_hp.gif');">
  17. <input type="button" onclick="RandomPlaceImg('http://images.google.com/intl/en_ALL/images/images_hp.gif');" value="put more images randomly">
  18. </body>
  19.  
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#8: Apr 27 '07

re: Placing something in a random location on a page


var x = document.body.offsetHeight-grab_image.height-5;
var y = document.body.offsetWidth-grab_image.width-5;this line here keeps the images completely visible , so it don't generate an image off the screen.
Newbie
 
Join Date: Mar 2007
Posts: 17
#9: Apr 27 '07

re: Placing something in a random location on a page


thx guys!!!!!!!!
Reply