Javascript 随机化容器中元素的位置

Javascript 随机化容器中元素的位置,javascript,jquery,css,random,Javascript,Jquery,Css,Random,我试图使用jquery/javascript在父容器中随机定位多个子元素。这是我目前掌握的密码。任何帮助或想法都会很好 如果你能让孩子永远不重叠,就可以获得额外的积分 HTML JQUERY $( document ).ready(function() { $('.population_man').each(function(){ $holder = $(this).parent(); $divWidth = $holder.width(); $divHeight = $

我试图使用jquery/javascript在父容器中随机定位多个子元素。这是我目前掌握的密码。任何帮助或想法都会很好

如果你能让孩子永远不重叠,就可以获得额外的积分

HTML

JQUERY

$( document ).ready(function() {
$('.population_man').each(function(){
    $holder = $(this).parent();
    $divWidth = $holder.width();
    $divHeight = $holder.height();

       $(this).css({
            left: Math.floor(Math.random() * $divWidth),
            top: Math.floor(Math.random() * $divHeight)
       });        

})
});

jsiddle:

试试这段代码。在您的JSFIDLE上,我发现jquery无法找到的错误。单击框架和扩展下拉列表中的javascript按钮AddjQuery。之后,更新并再次运行。那么它应该可以工作了

$( document ).ready(function() {
    $( '.population_man' ).each(function() {

        $holder    = $(this).parent();
        $divWidth  = $holder.width();
        $divHeight = $holder.height();

           $(this).css({
                'left': Math.floor( Math.random() * Number( $divWidth ) ),
                'top' : Math.floor( Math.random() * Number( $divHeight ) )
           });        

    })
});

我的荣幸。快乐编码!
$( document ).ready(function() {
$('.population_man').each(function(){
    $holder = $(this).parent();
    $divWidth = $holder.width();
    $divHeight = $holder.height();

       $(this).css({
            left: Math.floor(Math.random() * $divWidth),
            top: Math.floor(Math.random() * $divHeight)
       });        

})
});
$( document ).ready(function() {
    $( '.population_man' ).each(function() {

        $holder    = $(this).parent();
        $divWidth  = $holder.width();
        $divHeight = $holder.height();

           $(this).css({
                'left': Math.floor( Math.random() * Number( $divWidth ) ),
                'top' : Math.floor( Math.random() * Number( $divHeight ) )
           });        

    })
});