/    Sign up×
Community /Pin to ProfileBookmark

Drag only with handle?

I am trying to make a list sortable by dragging into right position. But I want the dragging only to be possible using the “drag icon” to the right. The other text area should be either selectable or clickable.

https://jsfiddle.net/7qbmasw1/3/

Possible?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumDec 27.2021 — I got this working by making the span draggable only. Disadvantage: The feedback image visible during dragging doesn't include the list item but is showing the icon inside the span only. This might be fixed by cloning the li and position it near the mouse pointer.

https://jsfiddle.net/Sempervivum/2s4xgn5e/1/
Copy linkTweet thisAlerts:
@sibertauthorDec 27.2021 — > @Sempervivum#1640910 by making the span draggable only

Yes, this was exactly what I asked for. But the visual effect could be better. I found that one can set the <li> as temporarily draggable. I got this to work with two disadvantages. It work only with the first line. And you can drop it on the drag icon.

https://jsfiddle.net/kv15mo6q/

Any tip how to fix this?
Copy linkTweet thisAlerts:
@SempervivumDec 27.2021 — @sibert#1640928
>It work only with the first line.

The reason is that querySelector matches the **first** element only. You need to use querySelectorAll and a loop to do this for all elements.

>And you can drop it on the drag icon.

Take a look at my JS:
``<i>
</i> document.addEventListener('drop', function (event) {
event.preventDefault();
// if the target is the span or handle
// the element where the dragged element
// has to be inserted
// is the parent element li:
let ele = event.target;
if (ele.matches('span')) {
ele = ele.parentNode;
}
ele.style['border-bottom'] = '';
ele.parentNode.insertBefore(dragging, ele.nextSibling);<i>
</i>
``

It gets the parent element li when the item is dropped on the span or handle.
Copy linkTweet thisAlerts:
@SempervivumDec 28.2021 — I went a different way: Used the dragged li element as a drag image:

https://jsfiddle.net/Sempervivum/c8ea0nxr/

Disadvantage: The drag image is very faint and there is no control over it.

Or create and position a custom drag element or image. This way one has complete control over it's appearance:

https://jsfiddle.net/Sempervivum/03u1eyfo/1/

(Red borders only for debugging)
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...