如何让图片位置随机
将一个目录下的图片
遍历取出来
然后套上div
放在网页中
怎么能让他们在随机的位置出现呢
目前用id的方式只能做到一个
多了就不可以了。

解决方案 »

  1.   

    用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});
       });})
      

  2.   

    Math.random()产生随机数后,你就随意搞了。
      

  3.   

    非常感谢前辈的回帖我就是页面onload的时候触发我想给每个图片都放在div中
    给一个id
    那个屏幕宽度我知道就是怎么能让同一id的div 位置随机
      

  4.   

    如果只有一个图片是可以的 但是图片超过一个 就不行了
    用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>
      

  5.   

    <STYLE TYPE="text/css">
    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>
      

  6.   

    我这个三呢 是个例子
    其实有很多图片
    我的意思是 怎么能取出来这个index数呢
    就是取出在一个页面 id相同的元素呢
    因为照片的名字是随机上传
    不可能排列递加
    非常感谢啊。
      

  7.   

    绝对 定位 设置 top lef 就可以
    你是怎么获取到图片集合的啊?你遍历时就可以以设置 坐标撒
    也可以用 document.getElementsByTagName("img") 来获取img 集合
      

  8.   

    但是我网页上还有别的图片呢
    怎么能用img随机呢
    只随机位置从文件夹取出来的图片
    我的意思是给这些图片一个id
    然后随机他们
    我现在就是在想在遍历的时候写入但是不行 只随机最后一张写入的图片
      

  9.   

    晕了,你这个问题发了多少遍啊???给所有含有图片的DIV一个相同的CLASS,按照CLASS取就行了。不用设置ID。<STYLE TYPE='text/css'>
        .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>