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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2003
    Location
    new york
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Javascript Array involving random properties, need help

    Ok I would like to use this script to randomly change a property(position, in pixels) onmousedown, and the position property is to be referring to multible tables/divs/img's/ect... like 20 tables for example and I want them all to randomly change position in a 1-999 pixel range on the X and Y position... all 20+ change onmousedown anywhere in the document

    heres the script i got from somewhere, seems a simple random script..

    array.prototype.randomise = function () {
    var thisArrayLength = this.length;
    // copy over our array
    var inputArray = new Array();
    for (var i=0;i<thisArrayLength;i++) {
    inputArray.push(this[i]);
    }
    // randomly splice everything back into our array
    for (var i=0;i<thisArrayLength;i++) {
    this[i] = inputArray.splice(random(inputArray.length),1);
    }
    }
    Last edited by z15645; 12-17-2003 at 09:48 PM.

  • #2
    New to the CF scene
    Join Date
    Dec 2003
    Location
    new york
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well I editting into a completely different question...

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    Array.prototype.randomise = function() {
    
    for(var i = 0; i < this.length; i++) {
    
    if(typeof this[i].nodeType != "undefined" && this[i].nodeType == 1) {
    
    this[i].style.left = Math.floor(Math.random() * 999) + 1;
    this[i].style.top = Math.floor(Math.random() * 999) + 1;
    
    }
    
    }
    
    return true;
    
    }
    Hope that helps!

    Happy coding!
    Last edited by nolachrymose; 12-17-2003 at 10:30 PM.

  • #4
    New to the CF scene
    Join Date
    Dec 2003
    Location
    new york
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but how do I use this on tables?

    I've tried a few things but haven't got it to work..

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    ...completely PINTLESS....
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <style type="text/css">
    
    div.hopper {
                position: absolute;
                font: bold 12pt monospace;
                color: #fff;
                border: 1px black dashed;
                background: #000;
               }
    
    </style>
    </head>
    <body>
    <script type="text/javascript" language="javascript">
    
    var hoppinEls = ['e1' , 'e2' , 'e3' , 'e4' , 'e5' , 'e6' , 'e7' , 'e8' , 'e9' , 'e10' ,
     'e11' , 'e12' , 'e13' , 'e14' , 'e15' , 'e16' , 'e17' , 'e18' , 'e19'];
    
    var clientwidth = (window.innerWidth) ? innerWidth :
    			(document.body && document.body.clientWidth) ? document.body.clientWidth : 
    				740;
    
    var clientheight = (window.innerHeight) ? innerHeight :
    			(document.body && document.body.clientHeight) ? document.body.clientHeight : 
    				560;
    
    function getRandomX(elwidth)
    {
    	return Math.floor(Math.random() * (clientwidth - elwidth));
    }
    
    function getRandomY(elheight)
    {
    	return Math.floor(Math.random() * (clientheight - elheight));
    }
    
    function changeBG()
    {
    	document.getElementsByTagName('BODY').item(0).style.background = String(randHexTriplet());
    }
    
    function make_em_hop()
    {
    	var el, e = 0;
    	while (el = document.getElementById(hoppinEls[e++]))
    	{
    		el.style.left = String(getRandomX(el.offsetWidth) + 'px');
    		el.style.top = String(getRandomY(el.offsetHeight) + 'px');
    	}
    }
    
    document.onmousedown = make_em_hop;
    document.onmouseup = changeBG;
    
    </script>
    <script type="text/javascript" language="javascript">
    
    function randSize()
    {
    	return Math.floor(Math.random() * 100 + 10);
    }
    
    function randHexTriplet()
    {
    	var h = 0, sHex = '#';
    	for (h; h < 6; ++h)
    		sHex += Math.floor(Math.random() * 16).toString(16);
    	return sHex;
    }
    
    var w, h, el, e = 0;
    while (el = hoppinEls[e++])
    	document.write(
    
    			'<div id="e' + e +'" class="hopper" ' ,
    			'style="width:' + (w = randSize()) + 'px;height:' + (h = randSize()) + 'px;' ,
    			'left:' + getRandomX(w) + 'px;' ,
    			'top:' + getRandomY(h) + 'px;' ,
    			'background:' + randHexTriplet() + ';">' ,
    			e + '</div>'
    
    		       );
    
    changeBG();
    
    </script>
    </body>
    </html>

  • #6
    New to the CF scene
    Join Date
    Dec 2003
    Location
    new york
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    THANKS , I LOVE IT!!!!!!!!!!!!!!!!
    Last edited by z15645; 12-18-2003 at 03:08 AM.

  • #7
    New to the CF scene
    Join Date
    Dec 2003
    Location
    new york
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can anyone explain the first part I posted, this:

    array.prototype.randomise = function () {
    var thisArrayLength = this.length;
    // copy over our array
    var inputArray = new Array();
    for (var i=0;i<thisArrayLength;i++) {
    inputArray.push(this[i]);
    }
    // randomly splice everything back into our array
    for (var i=0;i<thisArrayLength;i++) {
    this[i] = inputArray.splice(random(inputArray.length),1);
    }
    }

    like, am I supposed to type 'thisArrayLength' or replace it, same with 'inputArray' .. I don't know really what to replace it with either or how to implament the script to general pictures or div's or table's
    Attached Thumbnails Attached Thumbnails Javascript Array involving random properties, need help-atks.jpg  


  •  

    Posting Permissions

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