JavaScript How to place several elements randomly on page?

I want to place several elements of one class randomly on the page with Javascript / jQuery. My idea is to generate random values for margin-top, margin-left and z-index. The problem is that I need to generate values in between negative and positive (like from -150px to 300px) and I don't understand how to make it with the code I use:

$(".randomnumber").each(function () {
    var randomtop = Math.floor(Math.random() * 101);
    var randomleft = Math.floor(Math.random() * 101);
    var randomzindex = Math.floor(Math.random() * 101);
    $(this).css({
        "margin-top": randomtop,
        "margin-left": randomleft,
        "z-index": randomzindex
    });
});

http://jsfiddle.net/7JGqZ/651/

So the problems I have:

  1. Don't know how to make it work with negative values — for example I need the top-margin to be from -150px to 300px.

  2. The elements in the fiddle beave a bit strange, like their positions are not really random or like they're connected to each other...

Update:

Made it work, but still don't like the result, I actually would like elements to be placed randomly so they would fit the page size, I mean that elements would be placed all over the page, not going too far above the edge of the page. Now I have a parent element that is fixed in the centre of the page (has width and height = 0, top and bottom = 50%), and my idea was to position its child elements with generating top and left margins somehow like this:

$(document).ready(function(){
    $(".mood-img").each(function () {
        var height = $(window).height();
        var halfheight = height/2;
        var margintop = halfheight-400;
        var width = $(window).width();
        var halfwidth = width/2;
        var marginleft = halfwidth-500;
        var randomtop = getRandomInt(halfheight, margintop);
        var randomleft = getRandomInt(halfwidth, marginleft);
        var randomzindex = getRandomInt(1, 30);


        $(this).css({
            "margin-top": randomtop,
            "margin-left": randomleft,
            "z-index": randomzindex
        });
    });

    function getRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }
});

http://jsfiddle.net/7JGqZ/657/

Answer:1

I'm taking help from the code given here http://jbkflex.wordpress.com/2011/07/28/creating-a-svg-pie-chart-html5/ to create a pie chart. But the problem is, if I replace some of those values with 0. Or ...

I'm taking help from the code given here http://jbkflex.wordpress.com/2011/07/28/creating-a-svg-pie-chart-html5/ to create a pie chart. But the problem is, if I replace some of those values with 0. Or ...

  1. creating chart with two y axis
  2. creating chart with excel
  3. creating chart with vba
  4. creating chart with timeline
  5. creating chart in ms excel
  6. creating chart in google sheets
  7. creating chart of accounts
  8. creating chart in word
  9. creating chart in powerpoint
  10. creating chart in excel 2016
  11. creating chart from excel data
  12. creating chart in illustrator
  13. creating chart in excel 2010
  14. creating chart in indesign
  15. creating chart in numbers
  16. creating chart in python
  17. creating chart in excel 2013
  18. creating chart in ms word
  19. creating chart in html
  20. creating chart in ms excel 2007

I have tried the following: <body id="myBody" onload = "setTimeout('a()', 5000)" / > Is this the correct method? The reason why I want to do this is because I have my entire website animating ...

I have tried the following: <body id="myBody" onload = "setTimeout('a()', 5000)" / > Is this the correct method? The reason why I want to do this is because I have my entire website animating ...

  1. load javascript after page load
  2. load javascript after document ready
  3. load javascript after html
  4. load javascript after jquery
  5. load javascript after 5 seconds
  6. load javascript after ajax call
  7. load javascript after another script
  8. load javascript after page render
  9. load javascript after button click
  10. load javascript after body
  11. load javascript after dom
  12. load javascript after click
  13. load javascript after php
  14. load javascript after ajax
  15. load javascript after some time
  16. javascript load after all scripts
  17. javascript load after 2 seconds
  18. javascript load after
  19. javascript load after everything
  20. javascript load after scroll

I am learning node.js, and would like to try building a blog on the platform. Do you know of any good tutorials that show you, step-by-step, how to build a blogging app on node? I have seen one by ...

I am learning node.js, and would like to try building a blog on the platform. Do you know of any good tutorials that show you, step-by-step, how to build a blogging app on node? I have seen one by ...

Javascript, Jquery, HTML I am adding select options to a select box dynamically. I take each unique element in an array and add it as an option to the select element. It works great, but I need to ...

Javascript, Jquery, HTML I am adding select options to a select box dynamically. I take each unique element in an array and add it as an option to the select element. It works great, but I need to ...

  1. adding title attribute using jquery