-
Notifications
You must be signed in to change notification settings - Fork 0
/
socials.js
55 lines (42 loc) · 1.24 KB
/
socials.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Keep track of div positions
let divPositions = {};
function makeNewPosition(div){
// Get container dimensions
let h = document.querySelector('.circle-space').offsetHeight;
let w = document.querySelector('.circle-space').offsetWidth;
// Generate random x, y within container
let newY = Math.floor(Math.random() * h);
let newX = Math.floor(Math.random() * w);
// Check if new position overlaps with existing divs
let overlaps = false;
for(let key in divPositions) {
if(Math.abs(newX - divPositions[key].x) < 50 &&
Math.abs(newY - divPositions[key].y) < 50) {
overlaps = true;
break;
}
}
// If no overlap generate new position
if(!overlaps) {
divPositions[div] = {x: newX, y: newY};
return [newY, newX];
} else {
return makeNewPosition(div); // Recursively generate new position
}
}
// Rest of code remains the same
$(document).ready(function(){
animateDiv('.a');
animateDiv('.b');
animateDiv('.c');
animateDiv('.d');
animateDiv('.e');
animateDiv('.f');
animateDiv('.g');
});
function animateDiv(div){
let newPos = makeNewPosition(div); // Pass div selector
$(div).animate({ top: newPos[0], left: newPos[1] }, 5000, function(){
animateDiv(div);
});
};