Viewers: in countries Watching now:
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.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.