PDA

View Full Version : Javascript Array involving random properties, need help



z15645
12-17-2003, 08:42 PM
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);
}
}

z15645
12-17-2003, 10:06 PM
Well I editting into a completely different question...

nolachrymose
12-17-2003, 10:25 PM
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! :)

z15645
12-18-2003, 01:19 AM
Thanks, but how do I use this on tables?

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

adios
12-18-2003, 02:59 AM
...completely P:DINTLESS....


<!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>

z15645
12-18-2003, 03:06 AM
THANKS , I LOVE IT!!!!!!!!!!!!!!!!
:D :p :o :D :p :o :D

z15645
01-03-2004, 12:39 AM
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