javascript中div的随机位置
我正在尝试使用 javascript 使 Div 随机出现在网页上的任何位置。因此,一个 div 出现然后消失,然后另一个 div 出现在页面上的其他位置然后消失,然后另一个 div 再次出现在页面上的另一个随机位置然后消失,依此类推。 我不确定如何生成像素随机单位或使用什么技术生成随机位置。
我该怎么做?这是我的代码:
var currentDivPosition = myDiv.offset(),
myDivWidth = myDiv.width(),
myDivHeight = myDiv.height(),
var myDiv = $('<div>'),
finalDivPositionTop, finalDivPositionLeft;
myDiv.attr({ id: 'myDivId', class: 'myDivClass' }); // already defined with position: absolute is CSS file.
// Set new position
finalDivPositionTop = currentDivPosition.top + Math.floor( Math.random() * 100 );
finalDivPositionLeft = currentDivPosition.left + Math.floor( Math.random() * 100 );
myDiv.css({ // Set div position
top: finalDivPositionTop,
left: finalDivPositionLeft
});
$('body').append(myDiv);
myDiv.text('My position is: ' + finalDivPositionTop + ', ' + finalDivPositionLeft);
myDiv.fadeIn(500);
setTimeout(function(){
myDiv.fadeOut(500);
myDiv.remove();
}, 3000);
I'm trying to make Divs to appear randomly anywhere on a webpage with javascript. So a div appears then disappears, then another div appears somewhere else on the page then disappears, then another div appears again in another random spot on the page then disappears, and so on.
I'm not sure on how to generate random units in pixels or what technique to use to generate random positions.
How do I do that? Here's my code:
var currentDivPosition = myDiv.offset(),
myDivWidth = myDiv.width(),
myDivHeight = myDiv.height(),
var myDiv = $('<div>'),
finalDivPositionTop, finalDivPositionLeft;
myDiv.attr({ id: 'myDivId', class: 'myDivClass' }); // already defined with position: absolute is CSS file.
// Set new position
finalDivPositionTop = currentDivPosition.top + Math.floor( Math.random() * 100 );
finalDivPositionLeft = currentDivPosition.left + Math.floor( Math.random() * 100 );
myDiv.css({ // Set div position
top: finalDivPositionTop,
left: finalDivPositionLeft
});
$('body').append(myDiv);
myDiv.text('My position is: ' + finalDivPositionTop + ', ' + finalDivPositionLeft);
myDiv.fadeIn(500);
setTimeout(function(){
myDiv.fadeOut(500);
myDiv.remove();
}, 3000);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一种方法。我在固定范围内随机改变 div 的大小,然后设置位置,以便对象始终放置在当前窗口边界内。
编辑:为了好玩,添加了随机颜色。
编辑:添加了
.remove()
这样我们就不会用旧的 div 污染页面。示例: http://jsfiddle.net/redler/QcUPk/8/
Here's one way to do it. I'm randomly varying the size of the div within a fixed range, then setting the position so the object is always placed within the current window boundaries.
Edit: For fun, added a random color.
Edit: Added
.remove()
so we don't pollute the page with old divs.Example: http://jsfiddle.net/redler/QcUPk/8/
假设您有这个 HTML:
和这个 CSS:
使用 jQuery,如果您使用此脚本,每当您单击 div 时,它都会在文档中随机定位自己:
其工作方式是...首先计算
document
宽度和高度,然后计算div
宽度和高度,然后从document
宽度中减去div
宽度和div
距离document
高度的高度,并考虑您愿意放入div
的像素范围(因此它不会溢出文档)。如果div
上有内边距和边框,则还需要考虑这些值。一旦确定了范围,您就可以通过Math.random()
轻松地乘以该范围,并找到div
的随机位置。所以再一次:首先找到容器的尺寸,然后找到元素的尺寸,然后从容器尺寸中减去元素尺寸,然后对该值使用
Math.random()
。基本思想封装在这里:
http://jsfiddle.net/5mvKE/
Let's say you have this HTML:
And this CSS:
Using jQuery, if you use this script, whenever you click the div, it will position itself randomly in the document:
The way this works is...first you calculate the
document
width and height, then you calculate thediv
width and height, and then you subtract thediv
width from thedocument
width and thediv
height from thedocument
height and consider that the pixel range you're willing to put thediv
in (so it doesn't overflow out of the document). If you have padding and border on thediv
, you'll need to account for those values too. Once you've figured out the range, you can easily multiple that byMath.random()
and find the random position of yourdiv
.So once more: first find the dimensions of the container, then find the dimensions of your element, then subtract element dimensions from container dimensions, and THEN use
Math.random()
on that value.The basic idea is encapsulated here:
http://jsfiddle.net/5mvKE/
一些错误:
工作。
,就在您的 jQuery css 设置中:
Some bugs:
work.
Right in your jQuery css setup:
我通过这个为我们的网站更改了现有代码,您可以在 tweefox.nc 上看到它
I changed an existant code by this one for our website, you can see it on tweefox.nc