Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
How to use Math random() distribution three div?
Old 11-12-2010, 04:57 PM How to use Math random() distribution three div?
Novice Talker

Posts: 7
Trades: 0
I have 3 divs. I want distribution them random (when refresh the windows, three divs' position will be changed)
I think Math random() can solve this problem, but how to do that? Thanks.

Code:
<div id="div1">...</div><div id="div2">...</div><div id="div3">...</div><script>Math.floor(Math.random()*3)+1;...</script>
youlichika is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-26-2010, 11:58 AM Re: How to use Math random() distribution three div?
alexsts's Avatar
Novice Talker

Posts: 14
Name: Alex
Location: USA
Trades: 0
You can use random to switch number and then use Switch in JavaScript to load CSS you want.
Pretty good idea you can find in articles there:
http://www.howtocreate.co.uk/tutoria...ascript/domcss

Last edited by JeremyMiller; 11-26-2010 at 04:42 PM.. Reason: set link in reply
alexsts is offline
Reply With Quote
View Public Profile
 
Old 11-26-2010, 05:02 PM Re: How to use Math random() distribution three div?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Here's something to play with:
HTML Code:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Randomly Positioning Boxes On Page Load</title>

    <script type="text/javascript">
      function init() {
        //Set variables
        var i=1, //counter index
            corners={"top":0,
                     "left":0,
                     "bottom":300,
                     "right":300}, //Set the area within which the boxes' top-left corner will be positioned
            a_box; //variable to hold reference to a box
        //Search for all boxes with an id of box_#
        while(a_box = document.getElementById('box_'+i)) {
          a_box.style.left = (corners.left + (Math.random() * (corners.right - corners.left))) + "px";
          a_box.style.top = (corners.top + (Math.random() * (corners.bottom - corners.top))) + "px";
          i++;
        }
      }
    </script>
    <style type="text/css">
      .box_random {
        position:absolute;
        border:1px #000 solid;
        width:100px;
        height:100px;
      }
    </style>
  </head>
  <body onLoad="init();">
    <div class="box_random" id="box_1">Box 1</div>
    <div class="box_random" id="box_2">Box 2</div>
    <div class="box_random" id="box_3">Box 3</div>
  </body>
</html>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 11-27-2010, 10:36 AM Re: How to use Math random() distribution three div?
alexsts's Avatar
Novice Talker

Posts: 14
Name: Alex
Location: USA
Trades: 0
Jeremy, Nice coding and fun to use.
BUT...
Well, honestly I agree with old story that: when you give fish to a man you feed him for a day.
But if you give him a fishing rod and teach him how to use it-- he is set for life...
alexsts is offline
Reply With Quote
View Public Profile
 
Old 11-27-2010, 03:50 PM Re: How to use Math random() distribution three div?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Welcome to the forum Alex.

Take a look at the OP and you'll see:
  1. An attempt to try the problem.
  2. A clear lack of understanding about the DOM.
  3. And, some kind of attempt to actually learn (or there wouldn't be a Math.random() * 3).
In your response, the OP is guided to a tutorial on DOM CSS and given guidance to 'find a pretty good idea'. I'm sorry, but when I fish, I like a little more guidance on how to actually knot the line onto the hook as opposed to a line amounting to, "get some line, a hook, a book and an idea."

Now, let's take a critical look at what I posted -- Actually, hold the presses! That's exactly what I wanted to the OP to do: "take a critical look".

It is well known that the best way, for most, of learning a foreign language is via immersion. I see no reason why that cannot work for coding as well.

My code provided:
  1. A probably-unfamiliar doctype for the OP.
  2. The answer to the question.
  3. Comments to guide along the way -- without giving a full explanation.
  4. A bit more detail than was asked in the OP, requiring the OP to look at the code to truly achieve what was desired and in the process, learn more about how it all works.
  5. Inline JSON for variable declaration.
  6. Implicit type checking via a loop based on the assignment operator's return value.
  7. ... Well, I think you're getting the idea: An awful lot in but a few lines of code which will allow a number of opportunities to learn.
While some may think a link to read up on things is helpful; I oftentimes "show" in a manner which requires depth of thought to use and understand: Is not a picture worth 1000's words?

BTW: I really don't want to have this thread diverge from the OP and, while answering you, kept this post focused on the code provided and, therefore, on topic. If you really want to have a discussion about "how" people help, then please open a thread in the general discussion area and invite me to join.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to How to use Math random() distribution three div?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.38199 seconds with 12 queries