Drag and Drop Using Native Javascript 01
Skills:
JavaScript Fundamentals90%
Key Takeaways
The video demonstrates how to implement drag and drop functionality using native JavaScript, covering the basics of event handling and element manipulation.
Full Transcript
hey what's going on guys how all you are doing welcome to a new video tutor from IP today we're going to go ahead and go di into the drag and drop system so we're just going to implement a native JavaScript drag and drop system with no libraries no jQuery nothing like that just going to start it from scratch and try to implement something really really good so here's I'm using I'm going to use for this for this or for the sake of this video tutori going to use codepen.io for just the sake of Simplicity and to make it just faster and to just it can do the job for us you can use any code editor any I don't know like Visual Studio code to develop that but I'm just going to use code Ben making it simple uh like very simple so here's like the demo what we're going to achieve on this particular video tutorial so here's like the to- do items container so it like it holds the to-do items and whenever you Tak that as you can see you can drag it whenever you click it and the color change the background color change whenever you are holding that to the item and you can move it around and whenever you want to like put it in into the to-do container you get this highlight into the to-do container so this highlight this border thing which shows you where you are going to put these to-do items so it's pretty good as you can see here you can just drag it over all of the to-do container and just going to give you like an overview or a preview where you're going to put this toio item and whenever you're release that you're just going to snap right into the right position so takes this release it snaps into the right position so just going to go ahead and dive you can have like multiple to-do items in here from this container just take it and drag it and drop it on this to do container so just going to go step by step on how we can implement this the dragon drop and everything all the system just going to start working on it from the code so here we're going to start from scratch here I have like an empty JavaScript code going to start from the beginning and here's like the our HTML structure all it does we have like a container that holds all it's the main container under that container we have two main containers so the items container which is this one which holds the to-do items the second one which is the to-dos container which has the highlighting thing and which represent our to-dos items container so each one of these we have just header to shows the items and the to-do header and the second one we have the to-do item which is this particular item which we can actually going to be the target our Target where we can like drag and drop and place it into into end the to-dos container so here we're going to start I have like a predefined CSS style so you can find this in my uh like my website and do this particular tutorials uh like the written tutorial so just go ahead and do the description you going to find the link and you can just go ahead and grab this code or the style if you're really intending to use my style in here for for your uh dragon drop system but you can use your own it doesn't really matter much but here we can you can use the same thing so we have like a couple of things predefined like container base container and a lot of things so here we're just going to start by the JavaScript code so what we need is to reference these two containers the items container and the two containers so if you can take a look into the HTML structure so we can take and reference them so here we can use the items container which equals uh the document so document. get Elements by since we are using the ID in here which is items container so I'm just going to say Elements by ID I'm just going to say items container the second one we're going to like reference is the to-do container so just going to say to-do container and going to equals to the document again document don't get element by ID uh you can get the element by class but since they have or this since they share the same class name uh this is not really good to use class for that so here we have referencing them since we're going to need them that much also we need to use the to-do item or reference it uh but before that what we need is let's say that we're going to have like a to-do it here in this case in this particular one we have only one too item let's say we have like multiple to-do items and what we need is to just go ahead and drag or like hover and just put and drag and drop any item on this list so we are not going to support all only one item we need to support multiple items so for that we're going to need a loop and this Loop is just going to go ahead and loop through all the items just going to reference in here which has the to-do item class so let's just go ahead and reference that just going to be like the to-do item and I'm just going to say documents uh get Elements by the class name so since they share this CL the same class name over there as you can see in here so if you can take a look and the class name we have the to do item class name and if we have like multiple like to-do items in here diff containers they're going to share the same class name which means this function is going to return us an HTML collection so in HTML collection we can Loop through it and we can access each item differently or separately so we can do that and a for Loop and this should this should probably be to the items since we're going to like reference multiple items at once and here we're just going to need the four Loops so let's I equal zero I less than to do items. length and we're going to have i++ and I'm just going to look through each one of these so here we have got the loop we can now reference the one single item so let me just collapse all that so we can uh freely see my code and this very important thing so here we can reference that from this like take this like an array so you can reference in simply as an array normal array and here we've got one single item so what we need in the Jing Jin normally is we need to check for three major events so three important ones the first one is the click so whenever we click that we register this event or we check for this event the second one is the click and the draging or the click and the moving mouse moving so this event also is very important the last one is the drop which we just the mouse button or the left Mouse button up so we check for this three uh like major events so let's just go ahead and use that so we can register the event under the items so in here so this one item or this to do item we need to register the event under that so we can use the add event listener so here we can add an event and we're going to use the mouse down event so Mouse down events in here uh we can we can set which events in here you can use Mouse down Mouse move Mouse up y y and the second one is going to take the Callback so the Callback we're going to like create separate functions for this so let me just go ahead and create this on Mouse down and the first one is going to take the uh JavaScript events the second one is going to take our item in there and just going to use that and all of that so this like the three functions that we need on Mouse down on Mouse move and on Mouse up they uh three of them takes the uh the same parameters the events and the item that we are currently moving and here we just add the uster so on Mouse down we just run the on Mouse down and pass in the event in the item uh the same for the other ones Mouse move and mouse up we just call the function and we are good to go so just made this very very quickly for the sake of the video tutorial so you should already know how to do things like this in very basic way so now what we need is on the on Mouse down we need to trigger some uh Boolean so let's just go ahead and create this Boolean uh also I've created this mouse offset going to need it in here so to offset the actual position so the moving is going to be like POS so almost down um is going to equal to false so here it's going to equal to false in here whenever like we are holding the actual Mouse Down Under The Elements you are currently trying to move is just going to equal to True also if you have remember the effects that when we click that we get this really nice effect of the background we just the background changes so make it a little bit looks a little bit user friendly so for that we can go ahead and grab I have like a piece of code in here uh let me just grab it in there and here we're just going to change the background color to this color so we just going to make it little a little bit better now for like holding or keeping track of the offset what do you mean by the offset is whenever we click that whatever the mouse cursor is we're just going to register the mouse cursor position so we can hold it and we can later on whenever we need to move our item around the page is we can use this item offset so here we can use or we can say mouse offset equals new object the x is going to equal to item do offset left so we can get the offset left and we can minus it from the e client X and E client X represent the actual client cursor position or Mouse position uh the X position pretty much and then second one is going to be like the Y item do uh offset uh top and just going to be like the minus e. clients Y and we are good to go now this should do the job perfectly for us also what we need into the mouse up is as you can see here whenever I click that I don't get the uh on Mouse up I don't get the like the original background reverted back into the to itm so I need to do that I need to revert the original background so for that I have this end the style so you can just go into the too item I can grab this hex decimal value and I can go in here and say item do style background uh color equals this one and we are good to go now here is the main part which is going to be like the mouse moving now we need to move the mouse around the page so we click we drag and then we drop so for the mouse moving what we need is to make this item follow the cursor which is set this item position into the cursor or the mouse cursor position so for that first we need to check if the actual Mouse is holding the actual items so that's why we have declared the mouse down uh variable so that going to keep track of this so here we go I'm I'm spelling this wrong so it is mouse down so if it's moused down now we can move things up also we need to use the e. prevent default this will prevent the default behavior of the uh the currently clickable uh elements in here we are clicking this one so just going to prevent the default Behavior whatever this going to be like a link or something just going to prevent anything any awkward option you don't really expect so just going to prevent this so make sure to you always use this whenever you try to handle some clickable elements now here under the mouse Dev what we need to do is style and we say the left so this is how you change the actual position of an element and also make sure to set the style of this or the position into the elements um style to from Chang it from relative to the obsolute so you can uh like actually change change the actual left and top which represent the left represent the X position of the element and the top represent the Y position of the element so make sure to trigger that and here what we can say is e do clientx plus the mouse offsetx and here we are just setting that also we need to use our plus pixels why we plus pixels because this returns a number this returns a number and this takes a string instead of number also this takes a string plus uh pixels or centimet or whatever like that so we're just going to plus that and concatenating a string with a number just going to return a new string so just going to assign a new string there also need to take care of the top position so we can say e do client y+ M offsety plus pixels and we are pretty much good to go so this will reference the actual Mouse y position so here we should be like probably good to go and we can move things up so let me just save things and if we go ahead and like hold things up hold the to-do item and drag it over the page we get this really nice dragable uh effects in here as you can see whatever we drag it over the page we get this really nice event so here we can drag things up also we can drop it but not actually so we can't fully drop it but you know we can we can work with it so to drop it we can only change from is mouse down from True to false so we can just uh remove the Handler of the is mouse down just set it to Fs in here now if you try to like move this drop it and here you go now you can drop it out of the mouse and drag it put it whatever on the page position so that was actually guys for this video tutorial also we have like a couple of bugs in here happens I don't know very awkward bugs things uh it just happens I don't know why why so move let me just move item so that was actually guys for this tutorial that was very simple thing just moving and handling or just covering the uh actually the draging the system how it works and just explaining the basic stuff and the setup that we need behind the scenes so in the next video Tor we just going to cover the all things that you have seen the demo the snap the highlights how you can just drag this and drop and just going to snap snap into the right position and yet yet a lot of things have been or has left for us to do on this particular Tut so thank you guys for watching I really really do hope you enjoyed and I will catch you all in the next video tutorial
Original Description
Read Tutorial:
http://go.ipenywis.com/ipenye321e
--- Official Links----
Official Website: http://ipenywis.com
Official Facebook: http://go.ipenywis.com/ipeny4907
Official Twitter: http://go.ipenywis.com/islem9a03
---Support US---
By Becoming a Patreon: http://go.ipenywis.com/ipeny5024
Playlist
Uploads from CoderOne · CoderOne · 42 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
▶
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Laravel CMS | N-01 | Getting Started and Environment Set Up
CoderOne
Laravel CMS | N-02 | Routes, Controllers and Views
CoderOne
Laravel CMS | N-03 | Dealing With Assets and Page Layouts
CoderOne
Laravel CMS | N-04 | Migration, Models and Adding Categories
CoderOne
Laravel CMS | N-05 | Showing Added Categories, and Session Messages
CoderOne
Laravel CMS | N-06 | Edit\Remove Categories using AJAX and Popups
CoderOne
Laravel CMS | N-07 | Add New Post
CoderOne
Laravel CMS | N-08 | Creating a Basic Laravel Blog
CoderOne
HTML5 Canvas For Beginners | Getting Started | #01 with Webpack
CoderOne
HTML5 Canvas For Beginners | Drawing Basic Shapes | 02
CoderOne
HTML5 Canvas | Drawing Text With Circles | Part 01
CoderOne
HTML5 Canvas | Text Animation and Circle Collision | Part 02
CoderOne
Complete Guide To Web Development In 2018
CoderOne
AJAX & JSON Explained | Examples
CoderOne
Visual Studio Code Web Development Setup and Extensions
CoderOne
Electron Build Desktop Apps HTML/CSS/JS - Getting Started 01
CoderOne
Electron Build Desktop Apps HTML/CSS/JS | Creating Windows and Processes 02
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Dialog, Sessions and Cookies 03
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Custom App Menu & Context Menu 04
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Package, Build and Deploy your App 05
CoderOne
Node.js Brief Beginners Introduction #01
CoderOne
Node.js How Modules Works & NPM #02
CoderOne
Node.js Working With Events and Event Emitter #03
CoderOne
Node.js File System (Read and Write) FS 04
CoderOne
Node.js Create a Basic Server Using Express 05
CoderOne
Json Web Token Authentication JWT Explained Securing API
CoderOne
CSS Flex Box Design | Practical Examples |
CoderOne
API How It Works With Practical Examples
CoderOne
MongoDB Getting Started 01
CoderOne
MongoDB Working with APIs and Practical Examples
CoderOne
Create a Command Line Interface (CLI) Using Node JS #01
CoderOne
Create a Command Line Interface (CLI) Using Node JS 02
CoderOne
Build React Apps | Introduction And Getting Started #01
CoderOne
Build React Apps | Working With Components And Multiple Rendering #02
CoderOne
Build React Apps | App State Management | Practical Examples #03
CoderOne
Build React Apps | Getting Started With Flux (Intro) #04
CoderOne
Build React Apps | Create a Clock Timer Using React 05
CoderOne
Build React Apps | Create a Clock Timer Using React 05 | PART2|
CoderOne
CSS Preprocessors Getting Started | SASS and LESS | Practical Examples
CoderOne
Regex Introduction | Learn Regular Expressions 01
CoderOne
PHP VS Node.js Which is Best For Web Development
CoderOne
Drag and Drop Using Native Javascript 01
CoderOne
Drag And Drop Using Native Javascript 02
CoderOne
Master Git (Version Control) in One Video From Scratch
CoderOne
Let's Learn The New Javascript ES6 Class Syntax
CoderOne
Let's Create A BlockChain On Node.js
CoderOne
Best Online Code Editors For Web Developers
CoderOne
Let's Create a Modern Login Form on React #01
CoderOne
Let's Create a Modern Login Form on React #02
CoderOne
Laravel CMS | N-09 | Admin Registration
CoderOne
Laravel CMS | N-10 | Login and Authentication
CoderOne
Let's Create a Twitter Bot (Listen and Retweet)
CoderOne
Rapid Webpage Creation With Emmet (HTML & CSS)
CoderOne
Create Popups and Modals Using Native Javascript, HTML and CSS
CoderOne
Promises and Callbacks on Javascript With Practical Examples
CoderOne
Create Collapses using Native Javascript, HTML and CSS
CoderOne
Let's Learn Typescript (Javascript Devs) | Getting Started 01
CoderOne
Let's Learn Typescript | Interfaces and Generic Types (The Typing System) 02
CoderOne
Let's Learn Typescript | Work With Classes, Modules and Enums 03
CoderOne
Let's Learn Typescript | React and Webpack With TS 04
CoderOne
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
Stop installing giant icon packages — write SVGs as source with a CLI + MCP
Dev.to · arialus
Frontend Engineers Are Quietly Becoming the New Overhead.
Medium · AI
Building an Interactive Fashion Hero with HTML, CSS & GSAP
Dev.to AI
Out-of-Order HTML Streaming Moves from JS Frameworks into the Browser
InfoQ AI/ML
🎓
Tutor Explanation
DeepCamp AI