Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,974 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- Detecting drag and drop support
- Using the HTML5 draggable and dropzone attributes and onDragStart event
- Exploring different events
- Receiving drops with or without a drop zone
- Creating a simple game
Skill Level Intermediate
This is actually the place where we left off in the previous movie. And so we have the one event being handled, the dragstart event, and it's handled by the handleDragStart function. And there is the body of the HTML there and again this is right where we left off in the last movie. So let's go ahead and open this in the browser and we will open this in Google Chrome and you'll see that when I grab the Rock it says drag started and that's all it really does. So what we want to do here is we want to look at the different events and how they work.
And so the easiest way to do that is to write a generic event handler and just plug it into the different events and look at what happens. So here's our little generic event handler and it looks like this. We have a variable that counts the number of times an event is fired and you'll start at 1 and we have a simple function that will handle the event, an event handler function. So this is generic. I am just calling a handleDragEvent and the event itself, because we are not using it, I am just going to call it e and all it's going to do is display a statusMessage with the count.
And the little plus plus operator, all that does is every time the event counter is displayed it goes ahead and increments it for the next one. So it will show the next number and that's all there is to it. So that's our generic event handler. And now we just plug this in and I am just going to do that by making a copy of this one and paste in the copy, because I like copy and paste and instead of dragstart this would be for the drag event and handleDragStart will become handleDragEvent.
And so I am going to save this and all this is going to do is this is going to capture that DragEvent and for every time that DragEvent gets fired, it's going to display this counter and increment the counter so that it will display the next number, so 1, 2, 3, 4, 5, 6, 7, 8 etc. So let's go over to the browser now and reload and now when I grab this watch what happens. So now that counter-- and I am just holding it here in one place. As long as I have it dragged, as long as I have my finger on the mouse button, it's going to count and what that means is that event is getting fired over and over and over again.
So that's a good thing to know. Now we know something about the Drag event. When I drag something, as long as I have it dragged, wherever it is anywhere in the browser window, that counter is going to continue to get fired. That event gets fired over and over and over again in rapid succession as long as I have that object in its drag. So that's the drag event. Let's take a look at the dragend event. All we have to do now is change this to dragend and save and it's still using our same generic handler so that we can see what happens.
So when I switch back over hear to the browser and press Reload, now I get the drag started message. I am not getting that drag event itself anymore. Now I have a dragend event. So when I let go off my mouse button that ends the drag and I get count one and I drag it again. Now I get count tow. Every time I let go of the mouse button I get another count because that event is getting fired again. This is just a great way to look at all these different events and see how they work. The rest of the events actually happen on the destination rather than on the source.
So you'll notice our destination has a different id. This id img1 is for our source. Our destination is a drop zone. This over here is actually the whole div rather than just the image, because I am going to drop it anywhere in this div, and that has an id of dz1 for drop zone 1. And so I am going to change my element call to call dz1 and I am going to change my event to dragenter. So now when I drag the object into the drop zone and the object actually crosses the boundary into the drop zone, that's when it's going to fire the event.
So we will take a look at what that looks like. I am going to press Reload in the browser here and now I have got drag started and I am going to drag this over. You see it says count: 1 because I dragged it in and I can hold it in there and move it around and leave and come back and I get a number 2. And I leave and I come back and I get a number 3. Now if I do this really slowly, you will notice that I can get several at once. So it's not entirely reliable that it's going to be one call. It might be a few calls every time something and depending on how slowly they do it and how many times they actually cross that border on the way in.
So that's the dragenter event. Likewise there's a dragleave event. Let's save that and I am reloading the browser. And so I have dragged in and nothing happened. Now I am going to drag out and I get a 1. When I dragged in I got another 2 because my mouse must have crossed that border twice along the way. And here I can run it past and only get 1. So again you just want to get an idea of how this works so that when you write your code you can handle all of these different contingencies.
And so that's dragleave and finally dragover. You will notice that that works very much like the drag event did because you sees it says drag started. When I drag the object over the zone you will notice that the counter just keeps going. As long as I hold it over that zone, that dragover event fires over and over and over again. So that's also good to know. So there's also an event for drop and this lets you know when an object is dropped over your drop zone.
And just change this to say drop and I am going to save it and you will notice that something doesn't happen that you expect to happen. What you would expect to have happen is that when we drop the object over the drop zone is that this event will get fired. And you will notice I am going to reload this and I am going to just drop this object over the drop zone and you notice it still says drag started. We didn't get a count for that. We would expect this to be counting 1, 2, 3, 4, 5, like that and that's not what's happening and the reason for this is actually very odd.
According to the documentation, what happens is the dragover event has a default action. Each of these events has a default action. For the most part we don't care about the default action, but the default action for the dragover event actually resets the drag and the impact of this is that the drop event never gets fired and the only way to get the drop event to fire is to stop the dragover event from executing its default action. And you read all of the blogs and you read all of the commentary on this particular set of APIs and everybody scratches their head and says "I have no idea why this is the way that it is." Probably has something to do with the original implementation back at Microsoft and the person who made that decision for whatever reason isn't telling us why they made that decision.
For most of us it just seems dumb, but it's just the way that it is. So the way to handle this is to write a specific event handler for the dragover event and to intercept that default action and make it not happen. So here's how that works. It's very simple and you are just going to need to do this whenever you need to capture a drop event, which is going to be pretty much anytime you need to implement drag and drop, so you are going to do this a lot. So I am going to call this handleDragOver. I am going to say if event .preventDefault.
So if there is a method called preventDefault in the event object then we are going to call that method. Now there are some implementations which do not have the preventDefault and instead they work just by having this function return false. So this is what this looks like. It doesn't have to make any sense; you just have to know that it works. And then we need to add an event listener that calls that and so I am going to cut and paste here.
This is dragover event and it has to call our dragover and that will be on the destination like that and so we will save that. And so now what's going to happen is every time that dragover event is called, instead of resetting the drag, it's going to call this preventDefault function or return false depending on what browser we are using, and that will allow the drop event to fire.
So we will get our little counter. So what we expect to see happen is that every time we drop this rock into the drop zone we will get a count. There it is, count:1, count:2, count:3. So oddly enough, that's what it takes to make this work. You have to handle your dragover event or your drop event will never get fired. And in your dragover event you have to call this preventDefault and/or probably both of them, return false, and that will prevent the drag from getting reset and it allow your drop event to get fired.
So now we understand how all of these events work and we can take this knowledge and we can go ahead and start using them.