Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Adding touch events


JavaScript: Events

with Ray Villalobos

Video: Adding touch events

Adding touch events provides you with in-depth training on Developer. Taught by Ray Villalobos as part of the JavaScript: Events
please wait ...
Adding touch events
Video Duration: 9m 6s2h 11m Intermediate Sep 17, 2013

Viewers: in countries Watching now:

Adding touch events provides you with in-depth training on Developer. Taught by Ray Villalobos as part of the JavaScript: Events

View Course Description

Learn about the different types of JavaScript events available to use in your applications, including traditional events like mouse, form, and window events, plus events that allow you to manage the DOM and HTML5 events that deal with media. Author Ray Villalobos shows how to propagate and register events during the capturing and bubbling phases, cancel an event or default behavior, and respond to clicks, mouse-overs, right-clicks, following the mouse, and loading images. Plus, Ray demos an audio jukebox project and a drag-and-drop game that shows JavaScript desktop and touch events in action.

Topics include:
  • Understanding event registration and propagation
  • Analyzing event properties
  • Creating and removing DOM elements with events
  • Removing an event
  • Canceling and preventing default events
  • Playing media events
  • Handling media pauses
  • Dragging and dropping
  • Adding touch events
Developer Web
Ray Villalobos

Adding touch events

So application's working pretty well for desktop machines, but this is the perfect type of app you might want on the tablet. And when I was doing this project, my daughter immediately want to play this one the iPad. Right now if you take a look at this project on the iPad. You'll notice that you can't really drag elements. It doesn't understand click events on the iPad. And the other thing is if you rotate this into vertical view, you'll see that the elements don't quite fit the screen. And when you rotate them back you'll see that they're also sort of too big, so we're going to have to adjust a few things to make this work.

So the first thing we need to do is fix an issue with the view port, so that whenever we rotate the device, it'll display it properly. The view port is how iPads and iPhones understand a browser's width. And normally in most web applications, you want to set the view port to whatever size your device would be. So we're going to do something unusual here, and what we want to do is, go over to the head section of our HTML page. And somewhere in there add in a meta tag, and the meta tag is going to have a name of view port.

And then we're going to set the content. To a specific width. Now as I mentioned in most web applications we want the width to be equal to the device width. In this case we're setting all of our measurements by absolute positioning as you see right on any of these images. So we want to set the width of our elements to width that encompasses the entire screen that we're trying to position these elements in. The one that works with this project happens to be 1200.

So that's what I'll set it. And the other thing that I need is another meta tag. This one is going to have a name of apple-mobile. Web app capable and the content is going to be set to Yes. Now, what this will do is when we pull this up in a browser, it's going to allow us to export this as a desktop item. So I'm going to save this and switch back over. So if we pull this back up into our iPad with this new code that I've inserted in there, it will look just fine when it comes up in my browser.

And now if I rotate the device Notice that it adjusts to the size of the device. So that's what the view port tag is doing for us. The other tag, allows us to save this as an application on our desktops. If I click on this icon right here, and I tap on, at the home screen. It's going to allow us to say this is an application on our home screen. So, I'm going to click on this add button. So, it's going to show, somewhere on our desktop like this. And if I tap on this application, it's going to come up in the browser that doesn't have any of the navigation that the browser window would normally have.

So that's the advantage of using those 2 tags. Let's go back into our code and add the ability to touch the element on the screen to our code. So we're going to go back into the script. Let's go ahead and close out the index and I'm going to hide these other elements here. And what we want to do is track another event so it's going to go down here with all the other events. That I'm using. And this event is going to be called touchstart. And of course I'm going to execute a function called touchstart that I'll have to create up here.

So, I'll add another function, and this is going to be called touchstart. It's going to receive an event and the first thing I want to do here is prevent that the fall behavior on a mobile device to the fall behavior when you tap and move, will be to scroll the page. And you're probably seen that when I were showing you the page at the very beginning of this video So we want to prevent that, so we use prevent default. And the next thing we want to do is create some variables for our artwork, so which art, and just like we had before.

Except that now when I'm touching I'm not going to have any of the variables that I'm setting up in any of these other events, because these touch events are going to be different than any of these move events. So I'm going to need to keep track of my element in its own variable, so I'll set Which are to the target of the event. So in this case the target would be the thing that I touched on. And then I'm going to create another variable, this variable's going to be called touch, and it's going to be set to the event and then touches, and then array value of zero.

So the way that touches work on an iPad, whenever you touch the screen it's going to create a finger touch. That's going to be tracked in a different array number, so the first finger put on the iPad is going to be tracked at this event touches. And then the array value of zero if you tapped somewhere else in the screen, it would be tracked on a separate array number. So this is going to give us the touch event at position 0 which will be our first touch. So then just like in other places, I need to track the position of my move.

So I'm going to create another variable called move offsetX. And that is going to to be equal whichart, the variable I just created. And then offset left minus The touch position and pagex. And you might be noticing that we're using things that are maybe not compatible with some other platform like old version of Firefox. But we don't really care in this case because I'm targeting right now the iPad and it will understand everyone of these x and y positions and dimensions.

So in addition to move offset x, I need to track move offset y. And you gotta be careful here that you update this to top and also this to page y. Now just like before we need to reset the z position so it's going to execute this function right here so that every element that I tap. Is going to be at the very top of every one of the other images. And then after that, I am going to set the z index of the element that I tapped on (NOISE), to be 10.

Alright, so then the next thing I need to do is add an event listener for my moving or my touch moving. And down here, I was adding an event in three different calls, right? But when I'm doing touches, I contract the event inside this touch start. So, I'll do whichart. And then add an event listener. And I want to track this time touch move. And then execute a function literal inside here. So I'm going to open this up.

And then do the rest of my code here. So now the postition of my element is going to be equal to touch. That pagex that's the position of the touch in relationship to the page and plus the moveoffsetx and the same thing for the y position. So, as the element moves I'm going to track the position dynamically. So, this touch move is going to execute while I'm moving the finger on top of the iPad.

And this touch right here. Should just be touch not touche. PageY move offsetY and then I need to set the positioning attributes of the element that I'm moving. equals Position X in this case, plus the word pixels or px, and then clean this up a little bit.

And then I'm going to do the same thing for the Y parameter here, so whichart style. Top when we do Y position, is going to be Y, and pixels as well. One more thing, I need to make sure that this passes the false parameter here, so that it bubbles properly, and I'm going to save this. I've uploaded this application into an FTP server, so that I can look at it with my iPad. And If I click on one of these elements and just drag them over I can start making the snowman just fine. So our touch events are working properly and now we can get busy building our snowmen or snowladies.

So our approach to touches is definitely different than what we did when dragging. We were able to create a touch start event and instead of creating separate listener for the other events. We just added in EventListener for the touch start, and then inside touch start we added an event listener to touch move. Notice that we didn't really need to drag a touch drop or a drop event, because as soon as we're done moving something, the element is already positioned in the right place. So with the exception of having to keep track of the touches, using touch events is no different than any other event.

Now that your application is done, it's time to go ahead and build your own drag and drop games. If you're a dad like me, you're going to be a super popular dad whenever you finish one of these.

There are currently no FAQs about JavaScript: Events.

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

* Estimated file size

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


You have completed JavaScript: Events.

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


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

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 ?

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:

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

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts. provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now

A trusted source for knowledge.


We provide training to more than 4 million people, and our members tell us that helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about 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

Sign up and receive emails about 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.