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

Starting to drag

From: JavaScript: Events

Video: Starting to drag

Its time to start working on the JavaScript and I am going to go ahead and delete this extra space for here from the body and just save this. We don't really need to do anything else with the Index HTML document and we're done also with the style.css. The only thing we need to work with is the script.js file, now what I'm going to do is put my functions into a self executing variable, this is called namespacing. And its a pattern popularized by Yahoo and essentially it works like this. You create a new variable. I'm going to call mine dragndrop and we set that to a function inside some parenthesis.

Starting to drag

Its time to start working on the JavaScript and I am going to go ahead and delete this extra space for here from the body and just save this. We don't really need to do anything else with the Index HTML document and we're done also with the style.css. The only thing we need to work with is the script.js file, now what I'm going to do is put my functions into a self executing variable, this is called namespacing. And its a pattern popularized by Yahoo and essentially it works like this. You create a new variable. I'm going to call mine dragndrop and we set that to a function inside some parenthesis.

(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.

Show transcript

This video is part of

Image for JavaScript: Events
JavaScript: Events

27 video lessons · 7995 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.