Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
(BLANK_AUDIO) And after that we add another parenthesis and a semicolon. And then we're going to put all of our code right here in inside this variable. The reason I'm going to do this is because I'm going to be using a couple of global variables. And this technique is going to protect any other script I might have in my HTML document from having an issue with any of my global variables. This is called creating a protected namespace. And another benefit is that if we ever need to get into one of our global variables, or anything else inside our code, we can do so with our dragndrop variable.
So here's the variables I'm going to need for this project. I'm going to create a variable called myX. You can probably guess what it does is the exposition of the current element, and of course if we've got X we need to do one for Y. So we'll call another variable myY, and the last variable that I'm going to need is called whichArt. And that of course is going to keep track of the element that I'm clicking and dragging. Now I'll explain why I use these a little bit later on. But, let's go ahead and get started with our first event listener.
So what I want to do is add an event listener and we don't really have a lot of HTML in our document, so we're just going to add this listener to the body tags. So it will start off like this. document.querySelector, and I want to get the body element. And then add an event listener to that. The first event that I want to track is the dragstart event. So far, in all the other examples, I created a self executing function and I'm going to do things a little bit different here.
So normally I would do function, and then something like this, right? And then I would put, like a false right after that but I'm going to actually put this in as separate function, this time. So, I'm going to call this function movestart. And then pass it along the false variable. This is an alternative way of creating your event listeners. It's a little bit cleaner and sometimes it works pretty well so I'm just going to show you how to do this in a slightly different way. So after we create that call to our event listener, obviously, we need to create this function. So the function is going to be called, as I said.
Movestart. And notice that we don't have any parentheses right here when we call it, but we do need them up here. And this function is going to get an event. What we're going to do in this event is call our whichArt variable, and set it to the target of the event click so, whatever we clicked on is going to be passed into this whichArt variable so e.target is the target of what we clicked on so we'll set it to that. And then what I need to do is use this myX and myY variables to set the position of this element as it's being clicked on.
So normally what we would do is set the myX variable for something like e.offsetx, but there's a little bit of a problem with just doing something like this. Let me do it how you might think about doing it so myX, and myY and then you would set it to the position of the event, where ever you click on the event, and set it to how far away from the event it was in the x and y direction. The reason this is not going to work is that this way of doing things is compatible with Webkit and IE but not with Firefox.
So we're going to have to use some additional code to make sure this is compatible with other browsers. So we're going to do an if statement here. It's going to be a little bit funky so undefined, and then. I'm going to set it e.layerX and otherwise to e.offsetX. And what this is doing it's actually a compressed if and then statement. And what it's doing is it's saying, okay I need to set this variable called myX. And I want to test to see if the offsetX variable exists.
Or, if it's undefined. If a browser does not support offsetX, then that value will be undefined. And so if it is undefined, and that's what this question mark is, we want to set the value of myX to the value of e.layerX. Because, whatever browser doesn't support offsetX will support e.layerX. And then otherwise. That's what this colon means right here. It will set it to e.offsetX. So, this is sort of similar to writing a complicated if statement. Something that would look like this if this.
(BLANK_AUDIO) Then do my X equals to e.layerX, right. Otherwise, then set myX to e.offsetX. But it's just a much more compressed way of writing this same code right here. So you're doing the same thing as an if statement, just a lot quicker.
So let's go ahead and copy this, and do the same thing with the y. So myX, myY, e.offsetY here, and e.layerY, and e.offsetY. So this is just going to make it compatible with more browsers. Now the last thing I need to do is set the element that I clicked on to be on top of other elements. So to do that, I need to use the style attributes, I'll say whichArt.style.zIndex and then set that to some number above what I had before.
So if you remember from the html, we set every one of these elements have a z-index of 5. So by my define the z-index of the current element to 10, I'm going to put whatever I click on on top of any element on screen. Let me go ahead and save this. And that's going to be it for now. Our project is not doing that much yet. But we set things up and we've done it by protecting our variables by using name spacing. And making our x and y positions more compatible by taking into account how different browser track the x and y positions.
In the next movie, we'll tackle moving and dropping our artwork.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100268 Viewers
56 Video lessons · 113238 Viewers
71 Video lessons · 82120 Viewers
131 Video lessons · 39410 Viewers
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.