- View Offline
- Exporting graphics from Photoshop
- Positioning elements with CSS
- Using the Roundabout plug-in
- Activating the carousel callback
- Detecting the image in focus
- Adding the final touches
Skill Level Intermediate
To create the animation effect for the captions that we saw in the introduction video, let's come over to carousel.js. Now after showCaptions, we are going to add another custom function. Let's type function hideCaptions, beginning and ending parentheses, beginning and ending bracket. So inside of hideCaptions, let's type a dollar sign, beginning and ending parentheses. Inside of the parenthesis two tick marks for a string literal, pound sign, captions.
We are going to target the captions div. Outside of the parentheses, we are going to type .animate, beginning and ending parentheses, semicolon. Inside of the animate parentheses, we am going to type two brackets, inside of the brackets, two tick marks for a string literal. We are going to type opacity. Outside of the tick marks for the string literal but still inside of the brackets, colon, 0. And then outside of the brackets but inside of the parentheses, 250 milliseconds, or one quarter of the second.
So next we are going to modify the showCaption function to bring that opacity backup. So inside of the showCaption function, after the setCaption variable, let's hit some returns. We are going to create a new variable, so var newHeight. We are going to set the equal to dollar sign, parentheses, inside of the parentheses, two tick marks for a string literal, pound sign, captions, outside of the parentheses .height, beginning and ending parentheses, plus, two tick marks for a string literal, then a semicolon, and inside of the string literal, px.
So what this is going to do is calculate the new height of the captions container after we have run these setCaption HTML inside of that object, and then we are going to add the px here for the CSS value. Now that we have calculated the new height, let's hit a Return, hit dollar sign, parentheses, inside of the parentheses, two tick marks for a string literal. We are going to type .caption_container, outside of the parenthesis, .animates.
beginning a new parentheses, semicolon. Inside of the animate parentheses, two brackets. Inside of the brackets, two tick marks for a string literal. I am going to type height. Outside of the string literal but still inside of the brackets, colon, and let's come up here and copy the new height variable, copy that to the clipboard. Let's paste them inside of the brackets. Outside of the brackets, but still inside of the parentheses for animate, comma, 500 for half a second. Then we are going to type another comma, space, function, beginning and ending parentheses, beginning and ending bracket.
This is what's known as a callback function. So after this animation takes place then this function is going to trigger, but this function won't trigger until after the animated state, so we will know that the container has animated to its new state before we fade in the text. And by the way, this caption container has the border on it for the stem, so this gives us the illusion of the stem actually growing. Now inside of the callback function, dollar sign, parentheses, two marks for string literal, pound sign, captions. Outside of the parentheses, .animate, beginning and ending parentheses, semicolon. Inside of the parentheses two brackets, two tick marks for a string literal, opacity. Outside of the string literal but still inside of the brackets, colon, 1, and then outside of the brackets but inside of the parentheses for animate, comma 250.
In order to hook these two functions into our custom buttons, let's come down and select hideCaptions' function name. Let's copy that. Let's scroll up and under our createCustomButtons, we are going to come up here and inside of the nextItem, click function, we are going to add the hideCaptions. And inside of the previous item click function we are going to add hideCaptions. So this means when we click on the next item or previous item, we are going to run the hideCaptions function and then run the custom roundabout function to animate to either next child or previous child.
The second thing we are going to do inside of these custom click events is add a callback to play the showCaption as well so that the buttons will also trigger the caption to be reset. So to do that, under the roundabout for animateToNextChild, outside of the string literal but still inside of the parenthesis, we are going to type comma, space, and let's come down here and copy the showCaption name without the parentheses. Let's paste that here and under animateToPreviousChild, comma, space, and paste that here as well.
So again, each button will now hide the captions, tell the roundabout to animate to either next or previous child, and then once that's done, it will run the showCaption function. So to test this, let's choose File > Save All, switch back to carousel.html, File > Preview in Browser. So now in the browser when I click on one of the buttons, let's click on the left button here, the first time this runs up, you will notice that it just sort of pops into place. But then as I continue to click it, we will have see the type fade away and if we get to a caption that's longer or shorter, we will actually see the animation of the stem growing to accommodate the new text that's being injected in.
We are going to look for images inside of the carousel area, .click, beginning and ending parentheses, semicolon. Inside of click we are going to type function, parentheses and brackets. We will split this on the brackets, and inside of here we are going to copy the hideCaptions function call and paste it inside of here as well. So basically what we are doing here is in addition to the carousel automatically creating click events on all of the teapots, we are also going to say if any of the images are clicked inside of the carousel, we want to run our function as well. And we want to hook onto the hideCaptions function, so that when we click the teapots they will run the same functions that the buttons are running.
Now let's choose File > Save All. I still have this file open in my browser, so I will switch back to my browser, hit Reload, and now when I click the teapots, I will see the content fade away, the stem will grow, and then we will bring the text back in as well. So now that we have the same behavior happening for both the buttons and the teapots, in the next movie we will look at setting the carousel to default start on a particular teapot and we will set the corresponding caption to show up on load as well.
Sign up for a Premium Membership to download courses for Internet-free viewing.
Watch offline with your iOS, Android, or desktop app.Start Your Free Trial
After signing up, download the course here or from the iOS/Android App.