Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Insert on random position

    Hello! How can i insert an image on a random position in body? I can't figure this out.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    ?? 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.

  • #3
    New Coder
    Join Date
    Nov 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    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.

  • #5
    New Coder
    Join Date
    Nov 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, it doesn't push anything out of its way. There's nothing else on the screen.

  • #6
    New Coder
    Join Date
    Nov 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •