任意位置的形状-碰撞

时间:2019-10-13 19:58:30

标签: jquery

我试图在div部分的任意位置放置3个形状

https://jsfiddle.net/nerh2o13/

如何避免形状之间的碰撞?

enter image description here

$('section').each(function() {
        for (var i = 1; i < 4; i++) {
            $(this).append('<div class="shape number-0'+i+'"></div>');
        }
});

$('.shape').each(function() {
     $(this).css({
        left : Math.random() * ($('section').width() - $(this).width()),
        top : Math.random() * ($('section').height() - $(this).height())
     });
});

0 个答案:

没有答案