I've got a script that lets you drag around a box called "draggable-container." There are also a couple of other things on the screen: a "window" and a footer. I'm wondering how I can make sure that when you're dragging the "draggable-container," you can't drag it over the "window" or the footer.
For example, if you start dragging the box and then try to move it over the "window" or the footer, I want it to just stop moving. How can I make this happen?
My code:
// appDrag.js
const draggableContainer = document.getElementById("draggable-container");
let isDragging = false;
let xOffset, yOffset;
draggableContainer.addEventListener("mousedown", (event) => {
isDragging = true;
xOffset = event.clientX - draggableContainer.getBoundingClientRect().left;
yOffset = event.clientY - draggableContainer.getBoundingClientRect().top;
// Add the "dragging" class to the entire container when dragging starts
draggableContainer.classList.add("dragging");
});
document.addEventListener("mousemove", (event) => {
if (isDragging) {
event.preventDefault();
draggableContainer.style.left = event.clientX - xOffset + "px";
draggableContainer.style.top = event.clientY - yOffset + "px";
}
});
document.addEventListener("mouseup", () => {
isDragging = false;
// Remove the "dragging" class when dragging ends
draggableContainer.classList.remove("dragging");
});
I have tried implementing boundary checks, etc, but they either just don't work, or just stop draggable-container from working.