用Jquery做的 不知道你是要用什么事件触发随机位置的,我就写个click的做个例子吧 $(document).readey(function(){ var sHeight=$("body").height();//取得BODY高度 var sWidth=$("body").width();//取得BODY宽度 $("div").css("position","absolute"); $("button").click(function(){ var leftWidth=parseInt(Math.random()*sWidth)+"px"; var topHeight=parseInt(Math.random()*sHeight)+"px"; $("div").css({left:leftWidth,top:topHeight}); });})
不知道你是要用什么事件触发随机位置的,我就写个click的做个例子吧
$(document).readey(function(){
var sHeight=$("body").height();//取得BODY高度
var sWidth=$("body").width();//取得BODY宽度
$("div").css("position","absolute");
$("button").click(function(){
var leftWidth=parseInt(Math.random()*sWidth)+"px";
var topHeight=parseInt(Math.random()*sHeight)+"px";
$("div").css({left:leftWidth,top:topHeight});
});})
给一个id
那个屏幕宽度我知道就是怎么能让同一id的div 位置随机
用for循环应该怎么弄呢
最好用id标识来做
我页面上还有很多div
<STYLE TYPE="text/css">
<!--#moveobj {position: relative; }-->
</STYLE>
<script language="javascript">
<!--
//定义函数moveit;
function moveit(){moveobj.style.top = Math.random()*1024;
moveobj.style.left = Math.random()*768; //-->
</script><body onLoad="moveit()">
<div id="moveobj">ok<img src="file:///Macintosh HD/iPhoto Library/Masters/2011/01/23/20110123-210708/Photo on 2011-01-23 at 21.06.jpg" width="21" height="34"/></div>
<div id="moveobj">ok<img src="file:///Macintosh HD/iPhoto Library/Masters/2011/01/23/20110123-210708/Photo on 2011-01-23 at 21.06.jpg" width="21" height="34"/></div>
<div id="moveobj">ok<img src="file:///Macintosh HD/iPhoto Library/Masters/2011/01/23/20110123-210708/Photo on 2011-01-23 at 21.06.jpg" width="21" height="34"/></div>
</body>
div {position: absolute; }</STYLE>
<script language="javascript">//定义函数moveit;
function moveit(){for(i=0;i<3;i++)
{
document.getElementById("moveobj"+i).style.top = Math.random()*1024 + "px";
document.getElementById("moveobj"+i).style.left = Math.random()*768+"px";
}
}</script><body onload="moveit()">
<div id="moveobj0">ok<img src="http://i0.sinaimg.cn/dy/2011/0121/2011121171946.jpg" width="21" height="34"/></div>
<div id="moveobj1">ok<img src="http://i0.sinaimg.cn/dy/2011/0121/2011121171946.jpg" width="21" height="34"/></div>
<div id="moveobj2">ok<img src="http://i0.sinaimg.cn/dy/2011/0121/2011121171946.jpg" width="21" height="34"/></div>
</body>
其实有很多图片
我的意思是 怎么能取出来这个index数呢
就是取出在一个页面 id相同的元素呢
因为照片的名字是随机上传
不可能排列递加
非常感谢啊。
你是怎么获取到图片集合的啊?你遍历时就可以以设置 坐标撒
也可以用 document.getElementsByTagName("img") 来获取img 集合
怎么能用img随机呢
只随机位置从文件夹取出来的图片
我的意思是给这些图片一个id
然后随机他们
我现在就是在想在遍历的时候写入但是不行 只随机最后一张写入的图片
.random {position: absolute; }
</STYLE>
<div class='random'>xxxxxxxx<img src='' width=50 height=50 /></div>
<div class='random'>xxxxxxxx<img src='' width=50 height=50 /></div>
<div class='random'>xxxxxxxx<img src='' width=50 height=50 /></div><script type='text/javascript'>
//参数:div的类名
function setRandomPosOnClass(className){
var objs = document.getElementsByTagName('div');
for(var i in objs){
if(!objs[i].className)continue;
if(className.indexOf(objs[i].className)>-1){
objs[i].style.left=Math.random()*986 + 'px';
objs[i].style.top=Math.random()*700 + 'px';
}
}
}
setRandomPosOnClass('random');
</script>