|
|
@ -1,25 +1,25 @@
|
|
|
|
function randomPos(min, max) {
|
|
|
|
function getRandomPosition(min, max) {
|
|
|
|
return Math.random() * (max - min) + min;
|
|
|
|
return Math.random() * (max - min) + min;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let w = document.body.clientWidth
|
|
|
|
const bodyWidth = document.body.clientWidth;
|
|
|
|
let y = document.body.clientHeight - document.querySelector('footer').clientHeight
|
|
|
|
const bodyHeight = document.body.clientHeight - document.querySelector('footer').clientHeight;
|
|
|
|
let x_start = document.querySelector('header').clientHeight
|
|
|
|
const headerHeight = document.querySelector('header').clientHeight;
|
|
|
|
|
|
|
|
let currentZIndex = 1; // Initialize a variable to keep track of the highest z-index
|
|
|
|
document.querySelectorAll('.draggable').forEach(div => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
div.style.left = randomPos(0,w)+'px'
|
|
|
|
document.querySelectorAll('.draggable').forEach(draggable => {
|
|
|
|
div.style.top = randomPos(x_start,y)+'px'
|
|
|
|
draggable.style.left = `${getRandomPosition(0, bodyWidth)}px`;
|
|
|
|
|
|
|
|
draggable.style.top = `${getRandomPosition(headerHeight, bodyHeight)}px`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
draggable.addEventListener('mousedown', (event) => {
|
|
|
|
|
|
|
|
draggable.style.zIndex = ++currentZIndex; // Set the z-index to a higher value
|
|
|
|
|
|
|
|
|
|
|
|
div.onmousedown = function(event) {
|
|
|
|
const shiftX = event.clientX - draggable.getBoundingClientRect().left;
|
|
|
|
let shiftX = event.clientX - div.getBoundingClientRect().left;
|
|
|
|
const shiftY = event.clientY - draggable.getBoundingClientRect().top;
|
|
|
|
let shiftY = event.clientY - div.getBoundingClientRect().top;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function moveAt(pageX, pageY) {
|
|
|
|
function moveAt(pageX, pageY) {
|
|
|
|
div.style.left = pageX - shiftX + 'px';
|
|
|
|
draggable.style.left = `${pageX - shiftX}px`;
|
|
|
|
div.style.top = pageY - shiftY + 'px';
|
|
|
|
draggable.style.top = `${pageY - shiftY}px`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onMouseMove(event) {
|
|
|
|
function onMouseMove(event) {
|
|
|
@ -28,19 +28,15 @@ document.querySelectorAll('.draggable').forEach(div => {
|
|
|
|
|
|
|
|
|
|
|
|
document.addEventListener('mousemove', onMouseMove);
|
|
|
|
document.addEventListener('mousemove', onMouseMove);
|
|
|
|
|
|
|
|
|
|
|
|
div.onmouseup = function() {
|
|
|
|
draggable.addEventListener('mouseup', () => {
|
|
|
|
document.removeEventListener('mousemove', onMouseMove);
|
|
|
|
document.removeEventListener('mousemove', onMouseMove);
|
|
|
|
div.onmouseup = null;
|
|
|
|
}, { once: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
div.ondblclick = function() {
|
|
|
|
draggable.addEventListener('dblclick', () => {
|
|
|
|
document.removeEventListener('mousemove', onMouseMove);
|
|
|
|
document.removeEventListener('mousemove', onMouseMove);
|
|
|
|
};
|
|
|
|
}, { once: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
div.ondragstart = function() {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
draggable.addEventListener('dragstart', () => false);
|
|
|
|
|
|
|
|
});
|
|
|
|