Link to home
Start Free TrialLog in
Avatar of steam23
steam23Flag for Canada

asked on

jquery to position elements randomly, but without overlap

I am placing a dynamic amount of elements in random positions in a parent element. Each element is 75px square and position:absolute and I'd like to make sure that their random x and y co-ordinates don't overlap those of any already positioned elements.

Points for urgency...
$('.className').each(function(){
$(this).css({ 'top':randomXToY(0,375),
	 'left': randomXToY(0,450),
	});
});
 
// in case it's useful, my random function is just:
 
function randomXToY(minVal,maxVal,floatVal){
  var randVal = minVal+(Math.random()*(maxVal-minVal));
  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of alien109
alien109
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of steam23

ASKER

Sorry I hadn't accepted sooner... been wrapping my brain around it...

Thanks