Can I Make an Element Both Draggable and Clickable?
Asked by
0xAE_stack
I'm thinking about creating a blog where each post is represented by an image that users can drag around the page by holding the mouse down, but also click on to reveal the full blog post. Is it actually feasible to implement this? If it is, could anyone suggest some resources or examples to check out?
Answers
For sure, itโs possible, but it might lead to confusing user experience. Just keep that in mind as you design it!
Reply to this answer
Absolutely, itโs possible! Most websites allow you to click on links and drag them simultaneously. Just visualize a page where you can both engage and move elements. Itโs a neat concept!
Reply to this answer
You might want to explore react grid libraries; 'react-grid-layout' is a solid choice!
Reply to this answer
You can set it up using JavaScript like this: on click, process the click normally. On mousedown, start tracking position and set a flag for dragging. Then on mousemove, check if the element has moved significantly (like more than 5px) to determine if itโs a drag situation. Finally, on mouseup, check if it moved or not to decide whether to process a click or drop action.
Snuzzle: Drag events can help here! Use 'ondragstart' and 'ondragend'โthese trigger only after a drag motion starts, and you can set custom drag images, which is handy.
Reply to this answer
Yes, a click requires the button to be pressed down and released in the same spot. You can attach drag events and use `preventDefault()` to avoid triggering regular clicks when dragging. MDN has great examples for implementing this!
Reply to this answer
Yes, itโs doable, but examples might be tough to find. Think about how it could work on mobile devices, though! I suggest using an HTML5 canvas to avoid rendering issuesโthis will give you more control over the behavior you want.
Reply to this answer
Add your answer
Answers are reviewed before publication.
0xAE_stack: Thanks for confirming! I'll dive into some research based on this.