Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Dragging and dropping

From: JavaScript: Events

Video: Dragging and dropping

So we still have a long way to go on this project but we started to allow the user to drag elements on the page by listening to the drag start event. So this is really not letting you move anything but at least it's tracking the beginning of a move so we need to continue with this. Now before we do that, I wanted to talk to you about the different events that are available for dragging. And this is a really good article to read about all of the different events available to you when you're dragging things. So you can see that we have a drag start event, a drag event, drag enter, drag leave.

Dragging and dropping

So we still have a long way to go on this project but we started to allow the user to drag elements on the page by listening to the drag start event. So this is really not letting you move anything but at least it's tracking the beginning of a move so we need to continue with this. Now before we do that, I wanted to talk to you about the different events that are available for dragging. And this is a really good article to read about all of the different events available to you when you're dragging things. So you can see that we have a drag start event, a drag event, drag enter, drag leave.

Drag over, drop, and drag in. We're only really going to use a few of these in our project but take a look at this page to read more about some of these different elements. They're really designed to let you drag one thing onto another element. So if you look at the finished version of this, think it's somewhere down here, you can drag one item into another one. Let's see. There you go, like that. We're going to use it slightly differently. So let's go back into our code and take a look at what we need to do next. So we need to listen for another event, and the event that we're going to listen to in this case is going to be called drop.

So this is going to look exactly like our listener for the dragstart, but it's going to look for the drop event and then we're going to call a separate function. call moveDrop. Again, set it to false so that it bubbles probably, and then we're going to create the function called moveDrop. This is also going to receive an event. And If you don't do this, it's also going to let you drop an image into another image and it's sort of weird, some browsers kind of go crazy about that. So we're just using the prevent default method. From preventing any browser that doesn't exactly know how to handle drag and drop, from doing what it would normally do with an image.

So prevent default. And what we want to do now is position the element that we've moved into its new position. So to calculate that, I'm going to say whichart. So this is the art that I'm moving which I set to the target of this event up here. And I'm going to set that to style. Left and then it's going to be equal to e.pageX minus my variable called myX that I created up there and then plus the key word pixels.

Because this is CSS you're going to need to add px at the end. And then we're going to do the same thing for the top. And of course, this would be e page y. And this is where you make a lot of error with this, when you copy and paste some of these. Sometimes you forget to update one of these elements here. So you forget to update this x to a y, so watch out for that. Alright. Then we also need to track another event, and this event is going to be the drag over event. So we're tracking three things, dragstart, and I'll just copy and paste this drop event and I'll set this one in the middle to the dragover event.

And I'll execute this new function that I'm going to create called moveDragOver. So what dragover does is it keeps track of what's happening when you're dragging an element and you put it on top of another image. So we're going to create that function right here. And this function's not going to do much. It's going to prevent some browsers from acting funky. And I'm just going to copy this prevent default function. That's all we're going to do right here.

We're not interested in doing anything while the image is being dragged. And this is where you'd put code if you wanted to do something while it was being dragged. Is just going to prevent any image that you drag over from tracking that event. So actually I need to make sure that I name this moveDragOver. So let me do moveDragOver, and save this. And I'll switch over to my document, and refresh the page. And now you can see that when I roll over these elements, I get the little move icon, and if I click and drag any of these, it'll move to their new position.

And I can also click and drag em, and move them around wherever I want on the page. Not a lot of code here, but we've completed the click and drag portion of the code in order to get this working pretty well. The drag and drop API is really challenging if you're trying to keep things compatible with a lot of browsers. So make sure you're testing things in your target browsers as you go along.

Show transcript

This video is part of

Image for JavaScript: Events
JavaScript: Events

27 video lessons · 7732 viewers

Ray Villalobos
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript: Events.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.