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