Why Make Elements Draggable?
Draggable elements can significantly improve usability in various applications, such as:
- Dashboards: Users can rearrange widgets to suit their preferences.
- Image Galleries: Users can reposition images for better layout.
- Task Boards: Users can drag and drop tasks between different columns.
Implementing Draggable Elements
Let’s dive into the code! Below is a JavaScript function that makes an HTML element draggable. This function allows you to specify a handle element that initiates the dragging action, providing a more controlled user experience.
`function makeElementDraggable(selector: string, handleSelector?: string): void {
const draggableElements: HTMLElement[] = selector.startsWith("#")
? [document.getElementById(selector.slice(1)) as HTMLElement]
: Array.from(document.getElementsByClassName(selector.slice(1)) as HTMLCollectionOf);
draggableElements.forEach((draggable) => {
if (!draggable) return;
const handleElement = handleSelector ? draggable.querySelector(handleSelector) as HTMLElement : draggable;
if (!handleElement) return;
let isMouseDown = false;
let initX = 0, initY = 0, offsetX = 0, offsetY = 0;
function mouseDown(e: MouseEvent): void {
isMouseDown = true;
handleElement.style.cursor = "default";
offsetX = draggable.offsetLeft;
offsetY = draggable.offsetTop;
initX = e.clientX - offsetX;
initY = e.clientY - offsetY;
document.addEventListener("mousemove", mouseMove);
document.addEventListener("mouseup", mouseUp);
}
function mouseMove(e: MouseEvent): void {
if (!isMouseDown) return;
const x = e.clientX - initX;
const y = e.clientY - initY;
draggable.style.setProperty("left", `${x}px`, "important");
draggable.style.setProperty("top", `${y}px`, "important");
draggable.style.position = "fixed";
}
function mouseUp(): void {
isMouseDown = false;
handleElement.style.cursor = "move";
document.removeEventListener("mousemove", mouseMove);
document.removeEventListener("mouseup", mouseUp);
}
handleElement.style.cursor = "move";
handleElement.addEventListener("mousedown", mouseDown);
});
}`
How It Works
Selecting Elements: The function accepts a CSS selector to identify the element(s) you want to make draggable. You can specify either an ID (using #) or a class (using .). An optional second parameter allows you to define a handle element that will initiate the dragging.
Mouse Events:
- mousedown: When the user presses the mouse button down on the handle, we start tracking movement.
- mousemove: As the user moves the mouse while holding down the button, we update the position of the draggable element based on the current mouse position.
- mouseup: When the user releases the mouse button, we stop tracking movement.
- Positioning: The draggable element's position is set using CSS properties (left and top). The position is set to fixed, allowing it to be placed anywhere on the viewport.
To use this directly :-
makeElementDraggable('#myDraggableElement', '.handle');
In this example, #myDraggableElement will be made draggable using .handle as the drag handle.
Conclusion
Implementing draggable elements in your web applications can greatly enhance interactivity and user satisfaction. The provided code snippet offers a straightforward way to achieve this with minimal setup. Try it out in your projects and see how it improves your user interface!
You can do the same in react as well , create some custom hooks like useDraggable which will be doing the same thing.
Thanks ,BYEEE
Top comments (0)