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

Adding swipe gestures for mobile

From: HTML5 Projects: Creating a Responsive Presentation

Video: Adding swipe gestures for mobile

The cool things about creating your own responsive presentation is that not only can you take advantage of things that the user already expects, like using the arrow keys that they'd find in a PowerPoint presentation, but scaling down to devices, you can take advantage of other expectations like swipe gestures. So, let's go ahead and add a swipe library and wire up the swipe gestures to the navigation we already have. Let's start by going into the chapter 3 folder, 03_03, start folder, js, and open the pres.js file.

Adding swipe gestures for mobile

The cool things about creating your own responsive presentation is that not only can you take advantage of things that the user already expects, like using the arrow keys that they'd find in a PowerPoint presentation, but scaling down to devices, you can take advantage of other expectations like swipe gestures. So, let's go ahead and add a swipe library and wire up the swipe gestures to the navigation we already have. Let's start by going into the chapter 3 folder, 03_03, start folder, js, and open the pres.js file.

We're going to be adding more code inside a ready function to handle the swipe, but first let's download the library and make sure that library is incorporated in our project. There's a couple different libraries you can use. The one w're going to use in this case is TouchSwipe. If you don't have access to the exercise files, just do a search for jQuery and then TouchSwipe, all one word. And we want this minimize, this minified file. If you do have access to the exercise files, you can grab a copy from this JS folder.

I'm just going to select that, copy it and paste it into the JS folder that we are actively working on. So we're going to then reference this minified file. I'm just going to go back to the Finder here and open the index.html file in Dreamweaver. We want to add another script tag. We want to make sure that it is available before our pres.'s file, so let's add it after jQuery, because it also relies on jQuery. Add the script tag.

I'm going to set the source. And let's go ahead and just browse for the source and select that jQuery library. Click Open. There it is. Let's close the tag and save the index.html file. I'm going to switch over to pres. js and let's start adding this code. Now, this code is kind of a long chunk and it's a little bit complicated, so if you're worried about making mistakes or you'd rather just copy and paste it, you can grab it from the snippets file in this video's exercise folder.

So let's call the jQuery function again. This time we're going to look for that slides containers so we want to detect any swipes that happen on the container that holds all the slides. And the syntax for this is that you then call this swipe function and then we pass in the parameters that we're going to use. So this TouchSwipe library is rather advanced. There's a lot of features in it that you may want to play with on your own, but for right now we're just going to do a real simple implementation.

I'm going to open up this object with the curly bracket and for this swipe property, it's going to be a function. And we're going to type out all the parameters from the documentation, but we're only going to use one of them, for right now. You can of course take advantage of more of them on our own. The first one is an event and then direction, distance--these are separated by commas--and duration, and fingerCount. And then we're going to close that parenthesis and open curly bracket.

And I'm just going to hit another line to return, close the curly bracket, and I'm going to close the other curly bracket and also close this parentheses here, and end line with a semicolon. So here we want to detect for what direction the user has swiped, so we're going to add a switch statement. And again you can use if- else if you prefer with this. It's really up to you. So we want to switch on that direction.

We're going to add two cases. You can look for up-down if you'd like. In this case let's just look for left and right. So I've got case left and then a colon. I'm gonna tab in. So if that's the case, let's go ahead and call that next line. And then we're going to break after that so no other code gets executed, and we're going to go back a tab and write a second case. This one's going to be to the right, and a colon. Then I'm going to tab in and say back if the user happens to swipe the right, and we're going to break again and we should be all.

It looks good like I've got a closing bracket there that Dreamweaver added for me, so that should be all in place. So this might seem a bit counterintuitive that our swipe gestures effectively work in the opposite direction of our key events. And the reason that is is because when I say, if I click to the right of fingerCount here and I drag to the left, the motion--you'd think of it as maybe effectively throwing the current screen away. So in a left-to-right culture of reading English, fingerCount comes after direction for instance.

So as I'm pushing things forward, suppose this selection is my viewfinder, fingerCount comes into view, and that's a later item than, say, direction. So as I swipe to the left, I'm moving forward. If I swipe to the right, I'm kind of going back in space. So it's a little bit counterintuitive, but something to think about. And of course if you add up or down, you're going to find the same thing there. Switch back to the Finder and open up the corresponding index.html file in my browser.

And now in addition to the arrow keys working, if I click and drag, I should be able to swipe to the next screen. There we go. And it works if I go backwards too. All right. We're going a long way towards having a responsive presentation, in that we are dealing with the medium of the touchscreen. In the next chapter we're to modify the CSS to also take advantage of the different options on different screens.

Show transcript

This video is part of

Image for HTML5 Projects: Creating a Responsive Presentation
HTML5 Projects: Creating a Responsive Presentation

24 video lessons · 8817 viewers

Justin Putney
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 HTML5 Projects: Creating a Responsive Presentation.

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