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

Adding touch events

From: JavaScript: Events

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

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. WhichArt.style.left 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.

Show transcript

This video is part of

Image for JavaScript: Events
JavaScript: Events

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

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.