Random position of multiple divs

This is a discussion on "Random position of multiple divs" within the JavaScript Forum section. This forum, and the thread "Random position of multiple divs are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Feb 20th, 2008, 13:45
Banned Member
Join Date: Feb 2008
Location: Earth
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Random position of multiple divs

Hi everyone.....

I am trying to have multiple divs load in random positions...... I have managed to get one div to work
I am not sure what to add to the javascript to make it work.....

this is my script below......


---------------------------------------------------------------------------------------------------
<html>
<head>
<title>
Random Start Position Test
</title>

<style>
#myobj {background-color:#cccccc;visibility:hidden;position:absolute}
</style>

<script>
// Cross Browser DOM
var aDOM = 0, ieDOM = 0, nsDOM = 0; var stdDOM = document.getElementById; if (stdDOM) aDOM = 1; else {ieDOM = document.all; if (ieDOM) aDOM = 1; else { var nsDOM = ((navigator.appName.indexOf('Netscape') != -1) && (parseInt(navigator.appVersion) ==4)); if (nsDOM) aDOM = 1;}}
function xDOM(objectId, wS) { if (stdDOM) return wS ? document.getElementById(objectId).style: document.getElementById(objectId); if (ieDOM) return wS ? document.all[objectId].style: document.all[objectId]; if (nsDOM) return document.layers[objectId]; }

// More Object Functions
function setObjVis(objectID,vis) {var objs = xDOM(objectID,1); objs.visibility = vis;}
function moveObjTo(objectID,x,y) {var objs = xDOM(objectID,1); objs.left = x; objs.top = y;}

// Browser Window Size and Position
function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;}
function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement.scroll Left? document.documentElement.scrollLeft:document.body. scrollLeft? document.body.scrollLeft:0;}
function posTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement.scroll Top? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0; }

// Random Position Script
var xxx = Math.floor(Math.random()* (pageWidth()-230));
var yyy = Math.floor(Math.random()* (pageHeight()-50));

function start() {var x = (posLeft()+xxx) + 'px'; var y = (posTop()+yyy) + 'px'; moveObjTo('myobj',x,y); setObjVis('myobj','visible');}
window.onload = start;
window.onscroll = start;
</script>
</head>
<body>
<div id="myobj">This is a web page object.</div>
</body>
</html>


---------------------------------------------------------------------------------------------------



I hope u can help!


ben

d-e-p-i-c-t dot com
Reply With Quote

  #2 (permalink)  
Old Feb 20th, 2008, 21:43
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 22 Times in 22 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: Random position of multiple divs

Actually, script you have right there returns some funny errors. This bit of code right here:

Code: Select all
function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement.scroll Left? document.documentElement.scrollLeft:document.body. scrollLeft? document.body.scrollLeft:0;}
No idea why, but just a heads up.
__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)

Last edited by CloudedVision; Feb 20th, 2008 at 21:49.
Reply With Quote
  #3 (permalink)  
Old Feb 20th, 2008, 22:00
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 22 Times in 22 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: Random position of multiple divs

I was bored, so I fixed the mysterious errors (extra spaces were added for some reason) and made it work with multiple divs:

Code: Select all
<html>
<head>
<title>
Random Start Position Test
</title>

<style>
div {background-color:#cccccc;visibility:hidden;position:absolute}
</style>

<script>
// Cross Browser DOM
var aDOM = 0, ieDOM = 0, nsDOM = 0; var stdDOM = document.getElementById; if (stdDOM) aDOM = 1; else {ieDOM = document.all; if (ieDOM) aDOM = 1; else { var nsDOM = ((navigator.appName.indexOf('Netscape') != -1) && (parseInt(navigator.appVersion) ==4)); if (nsDOM) aDOM = 1;}}
function xDOM(objectId, wS) { if (stdDOM) return wS ? document.getElementById(objectId).style: document.getElementById(objectId); if (ieDOM) return wS ? document.all[objectId].style: document.all[objectId]; if (nsDOM) return document.layers[objectId]; }

// More Object Functions
function setObjVis(objectID,vis) {var objs = xDOM(objectID,1); objs.visibility = vis;}
function moveObjTo(objectID,x,y) {var objs = xDOM(objectID,1); objs.left = x; objs.top = y;}

// Browser Window Size and Position
function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;}
function posLeft() {return typeof 
window.pageXOffset != 'undefined' ? 
window.pageXOffset:document.documentElement.scrollLeft? 
document.documentElement.scrollLeft:document.body. scrollLeft? 
document.body.scrollLeft:0;}
function posTop() {return typeof window.pageYOffset != 'undefined' 
? window.pageYOffset:document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0; }

objects = new Array("obj1", "obj2", "obj3", "obj4");

// Random Position Script
var xxx = new Array();
var yyy = new Array();

for(i=0; objects[i]; i++) {
    xxx[i]= Math.floor(Math.random()* (pageWidth()-230));
    yyy[i] = Math.floor(Math.random()* (pageHeight()-50));
}

function start() {
for(var i=0;obj=objects[i], obj; i++) {
var x = (posLeft()+xxx[i]) + 'px'; var y = (posTop()+yyy[i]) + 'px'; moveObjTo(obj,x,y); setObjVis(obj,'visible');
}
}
window.onload = start;
window.onscroll = start;
</script>
</head>
<body>
<div id="obj1">This is a web page object. 1</div>
<div id="obj2">This is a web page object. 2</div>
<div id="obj3">This is a web page object. 3</div>
<div id="obj4">This is a web page object. 4</div>
</body>
</html>
I won't always be around to do your work for you
__________________
echo "Take it easy, ".$CloudedVision;
.links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; }
<quote>&quot;I think it's wrong that only one company makes the game Monopoly&quot; - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
Reply With Quote
  #4 (permalink)  
Old Feb 22nd, 2008, 04:17
Junior Member
Join Date: Jul 2007
Location: Los Angeles
Age: 31
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Random position of multiple divs

This was actually resolved on another site
Reply With Quote
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple rollover images change multiple images joshlindem Web Page Design 4 Apr 18th, 2008 09:11
multiple overlapping divs? Donny Bahama Web Page Design 24 Apr 23rd, 2007 20:23
Please help - position problem with nested divs DavidinLondon Web Page Design 1 May 21st, 2006 15:10
random benbramz Classic ASP 5 Jul 18th, 2005 05:01


All times are GMT. The time now is 01:20.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43