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.
|
|
|
|
|
![]() |
||
Random position of multiple divs
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
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 |
|
|
|
||||
|
Re: Random position of multiple divs
Actually, script you have right there returns some funny errors. This bit of code right here:
__________________
echo "Take it easy, ".$CloudedVision; .links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; } <quote>"I think it's wrong that only one company makes the game Monopoly" - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
Last edited by CloudedVision; Feb 20th, 2008 at 21:49. |
|
||||
|
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:
__________________
echo "Take it easy, ".$CloudedVision; .links { site: other-road-design; blog: only-nerds-allowed; project: resource-fish; organization: ARMIES6; } <quote>"I think it's wrong that only one company makes the game Monopoly" - <name>Steven Wright</name></quote>
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
|
![]() |
| Thread Tools | |
|
|
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 |