admin管理员组文章数量:1387195
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
});
});
/
So the problems I have:
Don't know how to make it work with negative values — for example I need the top-margin to be from -150px to 300px.
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);
}
});
/
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/7JGqZ/651/
So the problems I have:
Don't know how to make it work with negative values — for example I need the top-margin to be from -150px to 300px.
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/7JGqZ/657/
Share Improve this question edited Nov 28, 2013 at 22:24 Kostsei asked Nov 28, 2013 at 20:56 KostseiKostsei 993 silver badges8 bronze badges 8- 1 As for #2, there is no true random when it es to coding. As for #1, you could generate a random number from 0 to 450 then subtract 150... – Zach Saucier Commented Nov 28, 2013 at 20:57
- For #1 you could generate random numbers between 0-450 and then subtract 150 to get at your desired range. – aztechy Commented Nov 28, 2013 at 20:59
- Well, I've heard about that, but what I mean here is that elements are always placed in a group, not far from each other. – Kostsei Commented Nov 28, 2013 at 21:00
- read this article Math.random – Givi Commented Nov 28, 2013 at 21:02
- 1 jsFiddle Updated – Givi Commented Nov 28, 2013 at 21:06
1 Answer
Reset to default 7As for #1, you could generate a random number from 0 to 450 then subtract 150
$(".randomnumber").each(function () {
var randomtop = Math.floor(Math.random() * 450 - 150);
var randomleft = Math.floor(Math.random() * 450 - 150);
var randomzindex = Math.floor(Math.random() * 450 - 150);
$(this).css({
"margin-top": randomtop,
"margin-left": randomleft,
"z-index": randomzindex
});
});
Updated jsFiddle
If you want the elements to be randomly placed throughout the entire screen without overlapping the edges, you can use the following instead: Demo (I cleaned it up a bit too)
As for #2, there is no true random when it es to coding. Most randoms, like Math.random
are derived from the time. From Mozilla:
Returns a pseudo-random number in the range [0,1) — that is, between 0 (inclusive) and 1 (exclusive). The random number generator is seeded from the current time, as in Java.
The numbers are as random as possible in javascript. Google more information and alternates if you don't believe me
To demonstrate how well Math.random
works, I edited the project to allow any number of element, set by numElems
. Try it a few times, it works pretty well. Demo here
EDIT
I'd also remend using scrollHeight
and scrollWidth
as opposed to using jQuery's width()
and height()
because it is more inclusive, thus giving a more accurate top
value
本文标签: javascriptHow to place several elements randomly on pageStack Overflow
版权声明:本文标题:javascript - How to place several elements randomly on page? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744575720a2613612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论