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

Adding JavaScript interactivity

From: CSS: Transitions and Transforms

Video: Adding JavaScript interactivity

While CSS has gotten really good at moving elements around the page space--in 3D no less--it's not really intended for controlling user interactions. That's where JavaScript comes in. In this movie we'll link to the necessary JavaScript code that will power the HTML buttons inserted in the last movie and set the classes properly so that CSS can do its thing, to whatever extent possible, for any particular browser. In my code editor I have open the spotlight.htm file from the Chapter 5/05_03 folder of the exercise files.

Adding JavaScript interactivity

While CSS has gotten really good at moving elements around the page space--in 3D no less--it's not really intended for controlling user interactions. That's where JavaScript comes in. In this movie we'll link to the necessary JavaScript code that will power the HTML buttons inserted in the last movie and set the classes properly so that CSS can do its thing, to whatever extent possible, for any particular browser. In my code editor I have open the spotlight.htm file from the Chapter 5/05_03 folder of the exercise files.

Let's place our cursor after the, for now, sole CSS link and insert a script tag to bring in a downloaded copy of Modernizer. It's already placed in the _scripts folder. Modernizer, of course, is the terrific library that adds classes for specific CSS features, supported and non-supported, to the HTML tag. Which in turn allows designers to code their CSS rules appropriately for a wide range of browsers. So the script type= "text/JavaScript" source going for _scripts folder, bring in modernizer .2.5.3.min.

Let's close off our script tag and go on to the next include. Next, let's bring in a set of general JavaScript functions in the utlis.js file. This is a modified version of the code that David DeSandro used in his Carousel demo. So script type again text/javascript and the source, it's going to be _scripts and utils.js file. Again, let's close the script tag.

Now before we go on why don't we take a quick tour of the utils.js file? Up top are a small group of functions for adding and removing classes from a JavaScript library called PastryKit. This is followed by a function also from PastryKit that equates touch actions with the appropriate mouse ones, which is followed by a bit of code to set up an Event Handler. You'll see that code block starting on line 35. Next, there is the JavaScript code that uses Modernizer to see the feature compatibility of the browsers being used, and finally, the namespace is established during initialization.

Let's head back to our source code, and now let's link to a file with a few more targeted JavaScript functions, specifically designed for this page. I have these functions stored in a file called gallery.js. So script tag type text/javascript and our source is going to be the same, _scripts folder and the file we're bringing in is called gallery.js. Let's close off the tag, and again, I want to show you what's included here. So let's go open that up.

Now in the first line, it sets up a variable, transformProp, that's used throughout these functions. The first function that follows that sets the initial parameters, including the default orientation is horizontal. If for some reason you wanted your carousel to appear vertically at first, you would change the value that's on line 11, this .isHorizontal from true to false. The Carousel3D prototype transform function handles the actual rotation when the Next or Previous button is clicked. What may not be obvious is that rather than rotate all the images, it's actually the outer container, the carousel itself, that rotates.

Finally, all the way down near the bottom there is a setTimeout function that appends a ready class to the body tag when everything is set to go, kind of like a jQuery document ready type function. Speaking of jQuery, well, that's our final script tag to include, so let's head back to the source code, create a new, and I'm going to link to the CDN or content delivery network copy of the latest jQuery library. So we start again with the script tag, same type and the source is going to be a URL http://code.jquery.com/jquery.js.

Now I brought in jQuery, because that's the JavaScript framework I'm most familiar with, and I want to use it to add and remove classes for each of the figures as the Next and Previous buttons are clicked. This will be used to make all but the current front facing image disappear for older browsers, so we can get that slideshow effect and gracefully degrade the whole slideshow carousel. To accomplish this we're going to bring in the code snippet for the JavaScript function. So let's Open that up, so we can take a quick tour, and this is in the folder in the 05_03 folder called _snippets, and it's called click-js.txt.

So let's open that up, and I'm going to walk through the code, it's not terribly long, but I did want to give you a clear view of what's happening. So after we open the tag, the first thing that happens on line 2 is we declare a variable inc, short for increment, to 1. And then we set up the overall function which has two sub-functions, the first one takes place whenever the Next button is clicked, and the actual function code starts on line 6, where you can see, I construct the string for current figure, consisting of #fig + whatever the increment value is.

Now there are some special conditions, because I am working with a set number of images, and that's with a conditional statement next takes care of. So if the increment is 10 then I set nextFig to #fig1. So we are on the very last image and the next one is going to be the first image in the series, fig1. Otherwise, the string nextFig is going to be calculated by combining our set label with increment +1. Make sense right? Now the next two lines, 12 and 13, add and remove the showFig class from the appropriate place.

So because this is the Next button, showFig will be added to the next figure to be shown, and it will be removed from the current figure. Then on line 14, I increment the variable inc, and line 15 is another check to see if we've gone out of bounds or not. So if the increment has increased to 11, we reset it to 1. Now the next function, starting on line 18 comes in to play whenever Previous is clicked. This is essentially the exact inverse of the previous function. So again, I'm setting up currentfig to be the same thing, but now, instead of if inc=10, we check to see if the increment is at 1, and if it is then previous fig, preFig, is equal to 10 and otherwise, we set it up so that preFig is equal to my standard string #fig, plus the increment - 1, because we're going in the other direction.

Lines 26 and 27, add and remove the classes from the proper place, line 28 decrements the variable, goes back 1, and line 29 finally, checks to see that if we are at 0, we should make the increment 10. All right! So that's how that all works, now let's copy the code, head back to spotlight.htm, and we'll insert it right after that last script tag that we put in. Okay, our JavaScript's complete let's be sure to save the work before we move on to the finishing touch our transition and transform powered CSS.

Show transcript

This video is part of

Image for CSS: Transitions and Transforms
CSS: Transitions and Transforms

26 video lessons · 14137 viewers

Joseph Lowery
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 CSS: Transitions and Transforms.

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.