Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2013, 03:57 PM   PM User | #1
Sscorpius
New Coder

 
Join Date: Nov 2012
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Sscorpius is an unknown quantity at this point
Insert on random position

Hello! How can i insert an image on a random position in body? I can't figure this out.
Sscorpius is offline   Reply With Quote
Old 01-12-2013, 07:32 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
?? How random should it be? For example could it be inserted in between the letters of a single word? Or should the image overlay whatever already exists on the page?

I think you need to define what you are trying to do a lot better.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-12-2013, 08:16 PM   PM User | #3
Sscorpius
New Coder

 
Join Date: Nov 2012
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Sscorpius is an unknown quantity at this point
The image has a defined size, for example 40*40. When i click a button, it should appear randomly on the screen, i mean wherever on the screen.
Sscorpius is offline   Reply With Quote
Old 01-12-2013, 08:51 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Still didn't answer the question: Does the image push other content out of the way and insert itself into the page? For example, if your page was showing
Code:
This is a comment.
Would you want it to change to
Code:
This is a co[IMAGE HERE]mment.
Or do you simply want the image to appear *OVER* some random spot on the page?

"OVER" is trivial. Inserting is tougher.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-12-2013, 09:45 PM   PM User | #5
Sscorpius
New Coder

 
Join Date: Nov 2012
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Sscorpius is an unknown quantity at this point
No, it doesn't push anything out of its way. There's nothing else on the screen.
Sscorpius is offline   Reply With Quote
Old 01-12-2013, 10:21 PM   PM User | #6
Sscorpius
New Coder

 
Join Date: Nov 2012
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Sscorpius is an unknown quantity at this point
Take a look at this:

Code:
function generare()
{
var xmin=0;
var xmax=window.innerWidth;
var ymin=0;
var ymax=window.innerHeight;

var xcoord=Math.floor((Math.random()*xmax)+xmin);
var ycoord=Math.floor((Math.random()*ymax)+ymin);

var pui=document.createElement("img");
pui.setAttribute('src','pisoi.png');
pui.setAttribute('alt','pisoi');
pui.setAttribute('id','pisoi');
pui.style.left=xcoord+'px'
pui.style.top=ycoord+'px';
var pui2=pui.cloneNode(true);
var v=document.getElementsByTagName('body');

for (var i=0; i<3; i++)	 
    {
	 xcoord=Math.floor((Math.random()*xmax)+xmin);
	 ycoord=Math.floor((Math.random()*ymax)+ymin);
	 v[i].appendChild(pui2);
	 pui2=pui.cloneNode(true);
	 pui2.style.left=xcoord+'px'
     pui2.style.top=ycoord+'px';
	 }	
}
What i want to do is generate this image duplicated several times, on random positions on the screen, but this function generates the image only once.
Sscorpius is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:38 AM.


Advertisement
Log in to turn off these ads.