473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Placing something in a random location on a page

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!
Apr 19 '07 #1
8 7735
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!
Apr 19 '07 #2
devsusen
136 100+
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
Apr 20 '07 #3
mrhoo
428 256MB
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)
Apr 20 '07 #4
mrhoo
428 256MB
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. }
Apr 20 '07 #5
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!
Apr 26 '07 #6
iam_clint
1,208 Expert 1GB
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.  
Apr 27 '07 #7
iam_clint
1,208 Expert 1GB
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.
Apr 27 '07 #8
thx guys!!!!!!!!
Apr 27 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Papa Legba | last post by:
I have huge page of links, to whcih I am currently adding. I would like to add a "random link" button which would parse the current page, extract all URLs then load a random link. Before I start...
12
by: bhennon | last post by:
Hey all, I have a small php script that calls a random image at the following page. http://www.2006ymcanationals.com/random.php IT WORKS IF I go directly to the above link. I am trying to...
5
by: Martin | last post by:
I've found examples of bookmarklets that select a URL from a page at random, but I'd like one that generates part of the URL at random. If you have a bunch of images that are named:...
2
by: Art Kedroski | last post by:
We are using .NET validators on most of our aspx pages. When the validator is contained within a datalist (i.e. the EditItemTemplate) the WebUIValidation.js file location tag is not rendered on...
4
by: Alan Silver | last post by:
Hello, Seems everyone's site nowadays has those "Ads by Goooooogle" boxes on them. I would like to do something similar, so I can provide an affiliate scheme. I don't want to supply HTML for...
3
by: Beemer Biker | last post by:
I started seeing huge amounts of error messages such as "Validation (XHTML 1.0 Transitional):" Element schema is not supported". There was nothing wrong with that for 3 weeks and suddenly there...
1
by: Suudsu2200 | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.