JavaScript Drag & Drop Sortable List Project

JavaScript Drag & Drop Sortable List Project

Traversy Media

2 года назад

130,673 Просмотров

Learn the drag & drop API, array methods, spread operator, and DOM manipulation in this vanilla JavaScript project

Full 20 Projects Course:
https://www.udemy.com/course/web-projects-with-vanilla-javascript/?referralCode=F9B7C7FED834F91ADE75

Code: [MONTHNAME]2021

Code:
https://github.com/bradtraversy/vanillawebprojects/tree/master/sortable-list

TImestamps:
0:00 - Intro
2:27 - Insert List Items Into DOM
12:02 - Scramble List Items
18:44 - The CSS
31:07 - Drag & Drop Functionality
46:05 - Check Order

Тэги:

#javascript #javascript_DOM #vanilla_javascript #spread_operator #drag_and_drop #js_drag_and_drop
Ссылки и html тэги не поддерживаются


Комментарии:

@TraversyMedia
@TraversyMedia - 06.09.2021 19:05

Yes, I know the order of people is not correct anymore 😅 This was made a while ago. Feel free to change the names/order

Ответить
@au.voices
@au.voices - 31.10.2023 07:56

thank you for made this tutorial. i was doing it. but i have a problem, it cant be work in touch screen. can you help me how to make this also work in touchscreen??

Ответить
@pikboi956
@pikboi956 - 29.03.2023 14:22

how to delete drag?

Ответить
@thingsiwishiknew8592
@thingsiwishiknew8592 - 27.03.2023 12:54

Great video , really helping me understand how to implement the drag events as I am currently trying to implement this functionality to a get endpoint, so I can sort the response and push through the values/order to a Db. Any suggestions as the list I have will be dynamic depending on where clause?

Ответить
@PeriklesPeriklesoglu
@PeriklesPeriklesoglu - 20.03.2023 08:06

thanks

Ответить
@ganeshkharat118
@ganeshkharat118 - 27.01.2023 07:48

I want to mobile drag drop in javascript how can i do?

Ответить
@pharmaclick49
@pharmaclick49 - 19.01.2023 12:36

perfect

Ответить
@keremardcl6759
@keremardcl6759 - 12.12.2022 15:04

How can we re-sort the list rather than swapping positions?

Ответить
@priyanka_kofficial6579
@priyanka_kofficial6579 - 15.11.2022 14:15

Hi...Can you plz show with kendo ui...how we can draggable dropdownlist option

Ответить
@glitchspark1663
@glitchspark1663 - 11.11.2022 06:56

We can use Fisher-Yates to shuffle the array. That will be quicker.

function shuffle(array) {
const arr = [...array];
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}

Ответить
@lukmanm5922
@lukmanm5922 - 11.10.2022 11:48

have a question. Why is appendChild(itemOne or itemTwo) not adding to the existing content of the .draggable class. That was the behaviour i was expecting. Did it just replace the existing content?

Ответить
@SirNoviTheChauvi
@SirNoviTheChauvi - 28.09.2022 11:46

Why are you SWAPPING the elements around? I see that this is easily achieved but it's not really convient when you really want to order anything manually.
When would swapping be needed? When moving item 4 to position 1 your code puts item 1 to 4 instead of 2 .. when would you want this kind of functionality.

I don't get it.

Ответить
@bloodybritbastard
@bloodybritbastard - 15.08.2022 13:45

Thank you for this, I've been tearing my hair out on some tutorials, this is the first one that worked for me!

Ответить
@lukmanshaikh6675
@lukmanshaikh6675 - 19.07.2022 17:57

as i have tested its not working with mobile device can you pleas help in that ?

Ответить
@dchubad
@dchubad - 22.06.2022 08:09

what about saving the results so that when you refresh or load the page it remembers the new sort order?

Ответить
@wOkeN05
@wOkeN05 - 22.05.2022 12:23

This is by far the cleanest implementation of drag and drop I've ever seen 😍. Thanks for sharing this awesome technique, Brad🙂

Ответить
@ychoi405
@ychoi405 - 04.04.2022 06:50

How can I test the drag effects with my console window open? I can't see all the drag functions (I mean the console outputs to test to see dragover, dragenter, etc.) working in real time if I use chrome developer tool. With fire fox I can see the drag functions working, though. There should be a setup that I need to change in Chrome. so does anybody know about it?

Ответить
@photoinshot1355
@photoinshot1355 - 26.02.2022 17:12

How would you add a individual image to a name, say a photo of that person rather than the grab handles? I tried adding to the background using li:nth-of-type(1) and li:nth-of-type(2), etc adding one for each person. But obviously they do not drag when the li is moved. Any ideas?

Ответить
@TheOneHong
@TheOneHong - 16.02.2022 10:19

should use grab cursor instead of pointer cursor for dragging (personally thought)

Ответить
@Imaginativeone_DF
@Imaginativeone_DF - 14.02.2022 20:56

What's the best way to swap the cell you're using with ONLY the cell directly below it? Track the vertical (index) positions?

Ответить
@photoinshot1355
@photoinshot1355 - 02.02.2022 20:30

Thanks for a very interesting, informative, and instructional video. As always, the code logic is very well explained.

Ответить
@clintonmbonu9390
@clintonmbonu9390 - 15.01.2022 14:41

Thank you for this video! Saved me so many hours of trying to figure out how the drag and drop API works.

Ответить
@seppneimusser5723
@seppneimusser5723 - 08.01.2022 00:07

Your Tutorial is so amazing helpful for me, thank you so much...^^

Ответить
@dicidens69
@dicidens69 - 01.01.2022 03:53

hey dude, what is ur family font from vsc ?

Ответить
@georgsahliger7302
@georgsahliger7302 - 27.12.2021 18:13

Thx so much!

Ответить
@apophis2946
@apophis2946 - 24.11.2021 22:09

De-mystifying drag excellent

Ответить
@brad3201
@brad3201 - 17.11.2021 22:37

Thanks Brad! Your easy to follow solution helped me out a ton for a project that I am working on right now

Ответить
@Clockendmo
@Clockendmo - 15.11.2021 17:07

Elon Musk is crying as we speak

Ответить
@mangangure
@mangangure - 11.11.2021 00:46

I really enjoyed this one. Looking at how to extend the functionality

Ответить
@esku5472
@esku5472 - 25.10.2021 20:57

Can someone tell me what i need to know to get my first job as a Junior Front Dev? I know how to do basic stuff like creating simple games in Canvas, ToDoLists, Animated Navbars and i m right now trying to learn Async Await, where is the point when i will be good enough to apply for jobs, i just hate doing things when i don't know when there is that minimal cap.

Ответить
@heikokraemer2735
@heikokraemer2735 - 17.10.2021 13:42

Very nice tutorial, thank you! I've built a little game from it and than realized that is doesn't work on touch-screens. Found some blog-posts how to identify touch screens and gonna try to fix it and load the appropriate event listeners . Just thought I leave that comment here .. mobile drag & drop could be worth an other tutorial.

Ответить
@hassaneoutouaya
@hassaneoutouaya - 01.10.2021 21:56

Thank you Sir !
i'm just a little bit confused about how any changes occurs on the indexes of "list items" array, has to happen on "draggable list div" too, despite that there is no link between them.
to precise i'm talking about how swapping items on "list items array" affect the "draggable list div" despite that i don't see any relation between them ?
Thank you in advance !

Ответить
@jjbudinski8486
@jjbudinski8486 - 29.09.2021 02:21

Seeing classes that focus on the basics makes me happy, too many times there are multiple additional languages/systems that get slipped in right away which can be confusing.

Ответить
@andrewclarkemusic8570
@andrewclarkemusic8570 - 26.09.2021 13:19

can anyone explain how that sort worked??

Ответить
@0503trang
@0503trang - 24.09.2021 17:27

too hard for me :D

Ответить
@viniciusm.m.7822
@viniciusm.m.7822 - 22.09.2021 18:01

Hi!!
Would it be possible to "fix" the count of dragStart( ) event to 1 while been selected?
My english isn´t that good, im sorry. I´m from Brazil, learning a lot from you, thanks!!!

Ответить
@novailoveyou
@novailoveyou - 20.09.2021 12:29

Nice🤗

Ответить
@amirrezachegini1887
@amirrezachegini1887 - 20.09.2021 01:35

Nice

Ответить
@Gray_Studio
@Gray_Studio - 19.09.2021 17:21

Nice

Ответить
@sidagamhareesh3068
@sidagamhareesh3068 - 18.09.2021 08:42

Hey Brad can you try this in angular

Ответить
@nocturnal7346
@nocturnal7346 - 16.09.2021 14:45

I wonder what browser are you using? It looks really nice!

Ответить
@generationwolves
@generationwolves - 16.09.2021 10:47

An easier way to shuffle a list is to deduct 0.5 from Math.random() in the sort function.

Example: [1, 3, 110, 40, 302].sort( ()=> Math.random() - 0.5 )

- Math.random() generates random numbers between 0 and 1.
- Deducting it with 0.5 can either generate a + number or a - number.
- Passing the equation through the sort function will randomly place each item in front of or after the previous item, depending on the + or - value generated.

Therefore, you can refactor your code without both the .map() functions under createList() with this.
function createList(){
[...richestPeople]
.sort(() => Math.random() - 0.5)
.forEach( ... )
}

Ответить
@onelook1870
@onelook1870 - 14.09.2021 04:29

Brad Can you do a trello clone using react js

Ответить
@anujbhattarai3002
@anujbhattarai3002 - 12.09.2021 07:41

How to make the welcome page in new tab like yours ?

Ответить
@hackerprime9722
@hackerprime9722 - 12.09.2021 07:19

Much love from Jamaica.

Ответить