Create an Interactive Homepage Marquee with jQuery

Create an Interactive Homepage Marquee with jQuery

with Chris Converse

 


In this course, author Chris Converse builds a visually rich, interactive marquee to aggregate and display content on a web site home page. This course covers cropping and resizing artwork in Adobe Photoshop, creating HTML and CSS layouts, and dynamically generating marquee elements with the open-source JavaScript framework jQuery. Tutorials on animating between content panels are also included.
Topics include:
  • Creating project folders
  • Exporting graphics from Photoshop
  • Downloading and setting up Aptana Studio
  • Creating the HTML and JavaScript files
  • Setting up the Document Ready script
  • Generating the navigation buttons
  • Calculating and animating between photos
  • Setting the caption text
  • Preloading images and initializing the marquee

show more

author
Chris Converse
subject
Web, Web Design, Projects
level
Intermediate
duration
46m 49s
released
Mar 26, 2012

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Previewing the project across browsers and devices
00:04I'm Chris Converse, and this is a course on creating an interactive homepage
00:07marquee for your website.
00:09I want to start by first showing the final project that we're going to be creating.
00:13Inside of the downloadable assets, inside of Final Project, I'm going to open
00:17the index.html file, opening this up inside of a Web browser.
00:22What we have here is a preload on our project that we'll go through, and
00:27download all the images and make sure they're all loaded.
00:29Once it's loaded, the image will fade up and our caption will slide into place.
00:33We have these buttons down here that will automatically generate based on the HTML markup.
00:38As I click to links, the photos will sort of slide back and forth, the caption
00:41will move down, out of the way, change the content and then slide back up.
00:44Now, this entire experience is built using nothing more than HTML, CSS, and JavaScript.
00:51We've also used an open source framework called jQuery.
00:54One of the advantages of using an open source framework is that we can have the
00:57same user experience happen across all different platforms and browser types.
01:01So here we're going to go all the way back to Windows XP, going to take the same
01:04index file, let's load this into Internet Explorer 7.
01:08We do need at least IE7, so that we can have the transparency in the caption area here.
01:17Likewise, let's jump over to Windows 7 and let's take a look at the same project
01:21running in IE9 which shipped just a few weeks ago.
01:23Click on the buttons, photos slide, all the transparency is in place.
01:32In addition, we can also look at our project running across different devices as well.
01:36So if we take a look over at the Android platform, I'm going to come in here to
01:40Google on the Android platform, mobile phone, take a look at the same project.
01:44I'm going to hold Ctrl+F11 to rotate this device, tap and move this around.
01:52I can tap on the individual pieces here and get the same effect on a phone.
01:58Then likewise, running the same project inside of an iPad Simulator, I will hold
02:05Command+Arrow key to rotate this device.
02:07I can tap on these, and see these have same user experience again.
02:12One of the other features that we're going to be building into this particular project
02:17is these captions are going to be dynamic, so that regardless of how much
02:20content is inside of the caption area, we will calculate that using JavaScript
02:25and jQuery to figure out where the caption needs to move as our content grows.
02:29So I'm going to take the index file, open it up in a text editor, and I'm
02:34going to quickly come in, and take the content from the second caption, and
02:40just add two more sets of content here, so I've basically tripled the size of
02:44this particular object.
02:46Let's go back to our browser, let's hit Reload.
02:48Now, when I go to Slide 2, the content is going to slide up two-thirds taller to
02:54accommodate all the different content and then slide back down.
02:58In addition, the HTML, when it gets initialized is going to count, using jQuery,
03:03the number of panels that we need as well.
03:05So I can add or remove panels by simply adding or removing HTML in our source document.
03:09So I hope this project sounds interesting.
03:11And if so, let's get started with the first chapter.
Collapse this transcript
About the exercise files
00:00Now, when you download the Exercise Files, in addition to a copy of the Final
00:03Project that we looked at in the introduction video, there's also a folder
00:06called Art Templates.
00:08Inside of Art Templates, there's a folder for Photoshop Files.
00:10These have pre-sliced Photoshop graphics and the photography we're going to
00:13use for our marquee.
00:14There's another folder called Files to Add.
00:17We're going to be adding in copies of jQuery and a copy of the farinspace plugin
00:21that we got permission from, from Dimas Begunoff to include in these assets, and
00:25lastly, a snippets file that has some HTML that will sort of save us some time
00:29in putting the individual panels into our HTML markup.
00:33So once you have the Exercise Files downloaded, in the next movie, we'll start
00:36by creating the folders that are going to hold our project.
Collapse this transcript
1. Creating Project Folders
Creating folders for the project
00:00To begin our project, we're going to create a folder on the desktop.
00:03We're going to call this myMarquee.
00:10Inside that folder, we're going to create some additional folders, let me
00:13resize this for a moment.
00:15Inside of myMarquee, we're going to create a folder called images, another
00:22folder called includes, another folder called template, and another one called photos.
00:31Then, we're going to take the photos, and template folder, and put them inside
00:34of the images folder.
00:37And for the includes folder, let's come back to the Exercise Files and let's
00:40open inside of Art Templates, folder number 2 called Files to Add, open up
00:45includes, and let's grab a copy of the farinspace plugin, the jQuery plugin, and
00:50a copy of marquee.css.
00:53Let's Option+Drag a copy of these into the includes directory.
00:56This marquee CSS file will give us a jumpstart on the layout.
00:59Since the focus of this course is really on creating the actual marquee, I've
01:02gone ahead and created some of the CSS that we can hook on to the HTML to get
01:06the layout portion out of the way.
01:07So now that we have all of the folders and files in place for our project, in
01:11the next movie, we'll start by creating the graphics from Photoshop that go into
01:14the images directory.
Collapse this transcript
2. Preparing Web Graphics
Exporting graphics from Photoshop
00:00To create the graphics for our project, let's come back to the Exercise Files.
00:03Let's go into Art Templates folder, let's go into folder number 1, Photoshop
00:06Files, and let's open flags.psd in Photoshop.
00:09Once this opens, I'm going to open the window up here a little bit and zoom in.
00:12So I already have slices saved over top of each one of these flags.
00:16You can get to the Slicing tool by coming over to the Tool panel and coming
00:19under the Cropping tool down to Slice Selection tool.
00:22This allows me to double-click on a slice and see the properties.
00:24So in this case, I see this is the flag for England.
00:28Double-click this one.
00:29This is the flag for Italy.
00:32Since the slices are already in place, let's come up to the File menu, choose
00:35Save for Web & Devices.
00:38Once this dialog box comes up here, I can also click the slices and see the
00:41compression settings.
00:43Each of the flags is being set to a JPEG file with a quality of 80%.
00:47From this dialog box, let's come down and choose Save.
00:50Under the Slices, we want to choose All User Slices.
00:52This means any slice that we've actually created in the file will be saved and
00:56all of the other slices will be ignored.
00:57For the destination, let's choose Desktop/myMarquee/images directory, and
01:04let's pick template. Click Save.
01:07Once that's saved, let's close that Photoshop file, let's go back to the OS and
01:11we can see all of the individual flags in the template directory now.
01:13Next, let's go back to the Exercise Files, let's open nav_buttons.
01:17I'm going to zoom up on this as well.
01:21This graphic is actually going to be used as a CSS Sprite, meaning that we're
01:24going to have one graphic and we're actually going to move the graphic side to
01:27side based on the user interaction and these are going to be the navigation
01:30buttons that we looked at in the introduction video.
01:33So over in the Layers panel, let's turn on the rollover layer.
01:35Now, with the rollover layer turned on, what I want to have happen is that
01:39yellow color, I want it to be clipped into the circle.
01:41Let's come up to the Layer menu and let's come down and choose Create a Clipping Mask.
01:45This will make all of the artwork on the rollover layer clipped into the layer underneath;
01:49in this case, the circles.
01:51And then lastly let's turn on the dots layer, select our Move tool, and put the
01:56dot right in the center of the circle.
01:58Before we save this out, let's turn off the f.p.o layer.
02:02Then, we'll choose Save for Web & Devices.
02:03We're going to save this out as a PNG-24 with Transparency.
02:10Click Save, we're going to put this into the template directory as well, then click Save.
02:15Once that's done, we can close and save that file.
02:18Next, let's open marquee_caption.psd.
02:21This is simply a tab that's got rounded corners and it's semitransparent.
02:23We're going to save this for Web & Devices, 24-bit PNG, put it in the
02:31template folder, hit Save.
02:35And then lastly, we're going to open photos.psd.
02:38Now, inside of photos.psd, we have the different photograph on each of
02:41the individual layers.
02:43But, instead of saving out each of these individually, we're going to come up to
02:46the File menu, come down to Scripts, and choose Export Layers to Files.
02:52Once this comes up, under destination, let's choose Browse/Desktop/myMarquee,
02:58images, we'll pick the photos directory, click Open.
03:03For File Type, let's come down and choose JPEG, Quality is 8, no ICC color profile.
03:09Then, we're going to click Run.
03:10Photoshop will then activate each layer, save out a copy as a JPEG file, and
03:17then, back in the operating system, we can see in the photos directory a copy of
03:20each one of the photographs.
03:21One of the things we can't change is the fact that Photoshop will number each of
03:24the layers on export.
03:25So what we can do here is come in and simply rename these to take the number
03:29off of the beginning.
03:30So now that we have all of the graphics in place, in the next movie, we'll
03:33go into Aptana Studio, create a new project and create our HTML and
03:36JavaScript files.
Collapse this transcript
3. Creating HTML Markup
Downloading and setting up Aptana Studio
00:00In this course, we're going to be coding in a tool called Aptana Studio.
00:03You can follow along with any text or a Web editor that you choose, or if you
00:07want to download a free copy of Aptana Studio, you can go to aptana.com and
00:11download a copy for Macintosh or Windows.
00:13When you download and install this particular editor, one of the things that it
00:17has is the capability to change themes.
00:20By default, Aptana Studio 3 has this really dark background.
00:24I'm going to come up to the Aptana Studio 3 menu on the Mac, or you can choose
00:28the Preferences under the Window menu on Windows.
00:31Come down to Aptana, come down to Themes, and inside of here, I can change the
00:37themes that this is going to be using.
00:39For this course, I'd like to go back to Aptana Studio 2.x. This is going to give
00:43me a white background and it's going to let the color-coding really show and be
00:46a lot easier for you to follow along.
00:47Once I have made this change, I'm going to click OK.
00:51Now every time I launch Aptana Studio, it's going to use this particular theme
00:54instead of the darker background.
00:55Now that we have our editor set up, in the next movie we're going to start by
00:59creating a new project and continuing on.
Collapse this transcript
Creating HTML and JavaScript files
00:00Now we're ready to create a brand new project in Aptana Studio, so let's
00:03move over to Aptana.
00:05Now this course is also being offered using Adobe Dreamweaver, so if you prefer
00:08a more visual tool, you can find this same course in the Lynda.com Library using
00:11Dreamweaver inside of Aptana Studio.
00:13Now the first thing we're going to do in Aptana is create a new project.
00:16So let's go to the File menu, choose New > Web Project.
00:23We'll call this myMarquee.
00:26Let's uncheck Use default location.
00:28Let's click Browse > Desktop, and let's pick the myMarquee folder we created on the Desktop.
00:35Click Open.
00:36This warning here is Aptana's way of just letting us know that there are files
00:39in that particular folder.
00:41Let's click Finish and then in the left- hand side here, we'll see all of the
00:45files that were in there before we created the project from Aptana.
00:47Now the first file we need to create is going to be our HTML file, so
00:52let's choose File > New From Template > HTML, and let's choose XHTML
00:57Transitional Template.
00:58We want this file to work all the way back to IE7 and IE8.
01:02Right away, let's choose File > Save As. We'll call this marquee.html and let's
01:10save this in the myMarquee folder and click OK.
01:17Let's create our JavaScript file, let's choose File/New From
01:20Template/JavaScript/JavaScript Template, and then right away, let's save this.
01:27Let's choose Save As and let's put this into the Includes directory.
01:35We'll call this marquee.js, then hit OK.
01:39Now we need to hook in our CSS and JavaScript files into our HTML file.
01:43Let's come back to marquee.html, let's get our cursor after the title tag, let's
01:48hit a Return, and let's start by creating a link to our CSS file.
01:52So let's start with our link tag, <link rel--for relationship--="stylesheet"
02:03type="text/css" href="includes/marquee.css"/>.
02:17Next line, let's add a script tag.
02:28<script type="text/ javascript" src
02:33We're going to set the source= "includes--and the first JavaScript file
02:41we need to bring in is jQuery since the plugin for farinspace and our JavaScript
02:45is going to hook on to jQuery.
02:46jQuery has to be brought in first.
02:49Next line, another script tag, <script type= "text/javascript"src--we're
03:00going to set this one equal to ="includes/ farinspace/--and let's bring in the minified version.
03:13And then lastly, <script type="text /javascript" src=
03:22includes/--and let's bring in the marquee.js file which is going to be the one
03:28we're going to do our scripts inside of.
03:30Then, hit Command+S or Ctrl+S to save your document.
03:33Now that we have our HTML and JavaScript files created and we have the CSS and
03:37JavaScript hooked into the HTML, in the next movie, we'll work on the HTML
03:40markup for the project.
Collapse this transcript
Creating HTML for the container
00:00Now, the markup that we're going to create first is going to be for the
00:02outermost containers.
00:04So inside of the body area, let's hit a return, let's tab in and let's start by
00:08creating our outermost div container.
00:10So we'll do <div class.
00:12We're going to set class="ma and Aptana will bring up a code list
00:17of all of the CSS rules in that marquee CSS file we added, that start with ma.
00:21We want to come down and choose "marquee_ container," hit Return, then let's end the div tag.
00:27Then let's split the div tag open, inside of marquee_container the first item is
00:34going to be another <div class "marquee_photos"
00:39Let's come down and select photos.
00:42Let's end that div tag.
00:43Next line, let's start another div tag.
00:50<div class="marquee_caption" let's end that div.
00:57Next, another div tag.
01:00This one is going to be set to marquee_nav. <div class="marquee_nav"
01:04Let's end that div tag and then let's get inside of marquee_caption.
01:08Let's split that open and inside of marquee_caption, add another div.
01:13<div class="marquee_caption content" hit Return, end that div.
01:21And then lastly, outside of the marquee_ container altogether, let's add another div.
01:25We're going to set that equal to class. <div class=
01:30This is going to be "marquee_ panels" and then end that div tag.
01:34marquee_panels is where we're going to put all of the content that's going to be
01:37hidden from the browser with CSS, and that's where we're going to actually pull
01:40our data from with JavaScript to populate it into the marquee_container area.
01:44So now, let's split open the marquee_panels div.
01:51Let's choose File > Open File, from the Desktop, from the Exercise Files, let's go
01:58into Art Templates, let's open snippets.html.
02:01So, what we're going to do here is do Select All.
02:04Then, we're going to come in here and choose Copy under the Edit menu.
02:09Let's come back to marquee.html and inside of the marquee_panels div, let's hit Paste;
02:15Command+ or Ctrl+V. And now with all of the HTML markup in place, in the next
02:19movie, we'll start writing our scripts inside of the marquee.js file.
Collapse this transcript
4. Dynamically Generating Marquee Elements
Setting up the document.ready script
00:00Now, to start writing our JavaScript and jQuery statements,
00:02let's come up to marquee.js.
00:06So inside of here, I'm going to add a few line returns, and the first thing
00:08we're going to do is create our document ready.
00:10So let's start with a $()
00:12I'll type document.
00:16Outside of document, we'll type .ready();
00:23inside of the parentheses for ready, we're going to type function(){}.
00:29Let's split that open.
00:30So once all of our document data has been loaded, the first thing we want to
00:34do is create a photo lineup by going, and finding all of the photographs, and
00:37all of the panel divs and putting them into the photo div inside of the marquee container.
00:42So, to begin that process, let's type $()''
00:47tick marks inside for a string literal.
00:50We want to go search for img.marqee_panel_photo;
01:00so any image tag that has the marquee_panel_photo class assigned to it.
01:04Outside of the parentheses, we're going to type .each();
01:11inside of each, we're going to type function(){}.
01:17Let's split that open.
01:18Now, before we run our instruction for each item that we find, let's bring
01:23our cursor back inside of the parentheses for function, and let's type the word index.
01:29What this will do is make sure that jQuery counts each item that's found and
01:33assigns it an incrementing value.
01:36So the first thing we're going to do inside of this instruction, we're going to
01:39start by creating our own variable, photoWidth.
01:43We're going to set that = $()
01:49tick marks inside for a string literal, .marquee_container.
01:57Outside of the parentheses, .width();.
02:03So the photoWidth variable is going to be set to whatever the width of the
02:06marquee container is.
02:08This way, if we ever change the width of the container with CSS, we won't have
02:11to go back and change the JavaScript function as well.
02:15On the next line, let's create second variable.
02:22This one is going to be called photoPosition.
02:23We're going to set photoPosition = =index * --let's come
02:34up here and copy photoWidth variable name, paste it, then hit the semicolon.
02:42So we're going to multiply the incrementing value of index times the width of the photo.
02:47This is going to give us our photo lineup.
02:48Now, with these two variables in place, in the next movie, we'll actually
02:52write some HTML that gets injected into the photo div based on the values of
02:56these variables.
Collapse this transcript
Inserting photos into the marquee container
00:00Now with our two variables declared, let's hit a few line returns inside of
00:03here, and we want to inject some HTML inside of the marquee_photos div.
00:08So let's start by typing dollar sign, parentheses, tick marks inside for a
00:13string literal, .marquee_photos.
00:19Outside of the parentheses, we're going to type .append();, inside of append,
00:26two tick marks for a string literal.
00:28Let's start by typing an image tag, image, forward slash, and the tag.
00:35Inside of the image tag, let's add our attributes.
00:36First one is going to be class, equals, two quotes, marquee_photo.
00:48Outside of class, next attribute, style, equals, two quotes.
00:53Inside, we're going to say left: x;. Next, source, equals, quotes.
01:03Let's put a letter x in there as well;
01:06alt, equals, quotes, put a letter x. Next attribute, width, equals, quotes, 700.
01:18Next attribute, height, equals, quotes, 350.
01:21So now, for each marquee_panel_photo that's found as a class on an image, this
01:29each function is going to run, calculate these variables, and then we're going
01:32to put an image tag inside of marquee_photos matching that.
01:34Now, each image tag has to have unique properties set, which is why we calculated
01:40these variables up above.
01:41So the first thing we're going to do is set the left property.
01:44So let's come up here and copy photoPosition to the clipboard, under style,
01:50let's select that letter x we put in as a placeholder. Let's delete it.
01:54Put two single tick marks for a string literal.
01:57Inside of the tick marks, two plus signs, and then paste in photoPosition.
02:01So here, we're concatenating the value of photoPosition in each one of the
02:06image tags that we're writing into marquee panels as the value of the CSS style property left.
02:11Next, let's come over to the source x here.
02:15Two tick marks inside of here for a string literal, two plus signs.
02:19We want to go grab this source attribute from the original item found.
02:23So inside of here, we're going to type dollar sign, parentheses, this, outside
02:28of the parentheses attr for attribute, beginning and ending parentheses, tick
02:34marks inside of the attribute, parentheses for a string literal.
02:37Type src, so again, the original source attribute of the item found is going to
02:42be assigned to be the value of this source attribute that we're injecting into
02:45the marquee_photos div.
02:47For alt, let's come over here and select and delete the x for the alt, two tick
02:52marks for a string literal, two plus signs, dollar sign, parentheses, this,
03:00.attr, beginning and ending parentheses, tick marks inside for a string literal.
03:05Type alt, so the alt text that was on the original item will now be placed as
03:09the value of this alt item, and then after this instruction, let's hit a Return.
03:14Let's start a new instruction.
03:17Dollar sign, parentheses, tick marks for a string literal, .marquee_photos.css,
03:30beginning and ending parentheses, semicolon.
03:33Inside of the parentheses, two tick marks for a string literal, set
03:38width outside of the tick marks for the string literal, but still inside
03:41of the parentheses, comma.
03:44Let's come up and copy photoPosition.
03:46Let's come down here.
03:49Let's hit Paste, photoPosition, plus, let's come up here and grab photoWidth.
03:55Let's come down here and hit Paste.
04:00So what this script is doing is making sure that the width of the overall
04:03marquee_photos is wide enough to accommodate all of the images that we just
04:07injected in with each one of their style properties being set to be incremented
04:11value times the photo width.
04:12Now, to test our work so far, let's choose File>Save.
04:16Let's switch back to marquee.html, let's come up to the Commands menu.
04:20Let's come down to HTML, and let's choose Open in Default Browser.
04:24Now, in our browser, we'll see the very first photo being placed up here.
04:28We can also see a little bit of the caption showing up down below, and
04:32underneath, we can see all of the HTML markup that we added that defines each
04:36one of the individual panels.
04:39So if our outermost marquee panel wasn't clipping the photos, we would actually
04:42see four photos lined up all the way across the stage.
04:45But, in this case, we're clipping it down to only see the very first photo.
04:48So now that we know that the photography is properly being injected into the
04:52marquee container, in the next movie, we'll inject items for the navigation that
04:55correspond to the panels.
Collapse this transcript
Generating the navigation buttons
00:00Now, to create a navigation item for each corresponding panel, let's come
00:03back to marquee.js.
00:05After the panel photo each statement, let's hit a few Returns.
00:10Let's start with dollar sign, parentheses, two tick mark for a string literal,
00:16type marquee_panels .marquee_panel.
00:24Outside of the parentheses, type each();.
00:28So we're going to run in each statement on every panel we find in marquee panels.
00:35Now, just like up above, inside of each, let's type function(){}.
00:43Split the brackets open.
00:44Let's get our cursor inside of the parentheses for function.
00:47We're going to type index.
00:49Now, for each marquee panel that we find, we're going to type dollar sign,
00:53parentheses, tick marks for a string literal, marquee_nav.
01:01Outside of the parentheses, .append();.
01:08So what we're going to be doing is adding into the marquee_nav container our anchor tags.
01:14So inside of append, parentheses, tick marks for a string literal, let's type-in
01:18anchor tag, class, equals, two quotes, marquee_nav_item.
01:23
01:32Let's end the opening tag, and then let's put an ending anchor tag as well.
01:36So again, what this script is going to do is find all of the marquee_panel
01:39classes inside of marquee panels.
01:42For each one that it finds, we're going to append an anchor tag inside of
01:45marquee_nav container.
01:47To test this, let's choose File > Save.
01:48Let's go back to marquee.html.
01:51Let's go to Commands > HTML > Open in Default Browser.
01:55Now, we'll see our navigation items here being populated inside of the
02:00marquee_nav container and the same number of items that is being injected here
02:04matches the same number of items in the data.
02:06So now that we have all of our photos and navigation items being injected
02:11properly, based on the data, in the next movie, we'll activate the
02:14navigation buttons.
Collapse this transcript
Setting the navigation state
00:00To set up click events for the Navigation buttons, let's come back into marquee.js.
00:06After the each statement for all of the marquee panels, let's hit a few returns,
00:11let's type dollar sign, parentheses, tick marks for a string literal.
00:16I am going to type .marquee_nav a.marquee_nav_item.
00:26Outside of the parentheses, .click();.
00:34Inside of click, we're going to type function(){}.
00:42Split that open on the brackets.
00:43And what we're going to do for the click event, we're first going to search for
00:48all of the navigation items.
00:51So dollar sign, parentheses, tick marks, .marquee_nav a.marquee_nav_item.
01:04For each item, we're going to type on the outside of the
01:07parentheses .removeClass();.
01:14Inside of removeClass, two tick marks for a string literal.
01:17I am going to type selected.
01:20That's the name of one of the CSS rules in the CSS file that we included, and
01:24what the selected state does is change the position of the background to give us
01:27a dot in the center.
01:28So when the script runs, it's going to make sure that none of the navigation
01:31items have that class assigned.
01:33Then, on the next line, we're going to type dollar sign, parentheses, this
01:37inside of the parenthesis, so the actual item in this loop that is
01:41actually clicked on.
01:42We're going to type addClass();
01:46and then inside, two tick marks for a string literal, type selected. Let's choose Save.
01:58So what this is going to do is remove the classes from all of the navigation
02:01items, and then assign the selected class to the actual dot that we clicked on.
02:04Now, when we come back to our browser and hit Reload, now when I click on each
02:10one of these items, the item that I click on will get its selected state, and
02:14if there's a selected state on one of the other buttons, it will be removed.
02:16Now that we've got the navigation buttons indicating the one that's clicked, in
02:21the next movie we'll slide the photography back-and-forth to correspond to
02:24those buttons.
Collapse this transcript
5. Creating Animations Between Marquee Panels
Animating photos by calculating image position
00:00Now, in order to move the photos back- and-forth based on the navigation item we
00:03clicked, we're going to have to do a little bit of math, and that also means
00:06setting up a couple of variables.
00:08So inside of the click function we just created for adding and removing the
00:12selected state from the buttons, let's add a few more lines, and let's start by
00:15creating our first variable.
00:17So, we're going to type var navClicked = $(this).index();
00:34so whichever nav item in the group was clicked, we're going to know which one it
00:38is, and since there's four nav items, it will either be zero, one, two or three.
00:42Next variable on the next line, var marqueeWidth, we're going to set that equal
00:50to dollar sign, parentheses, tick marks for string literal, .marquee_container.
01:00Outside of the parentheses, .width();.
01:08Next, variable, distanceToMove, we're going to set that equal to, I'm going
01:17to come up and grab the marqueeWidth variable name, let's copy that to the clipboard.
01:24Let's paste it, marqueeWidth.
01:24We're going to multiply that, *();, inside of the parentheses, we're going to put -1.
01:37Since we're going to be animating the photography on the Y-axis, setting a
01:39negative number here is going to actually move the photography to the left.
01:42Next line, var newPhotoPosition, we're going to set that equal to, and we want
01:53to know which navigation item was clicked.
01:56So let's come up here and copy the variable navClicked.
01:58Let's come down here, paste it.
02:03Then, we're going to multiply that, times, let's come up here, and
02:07pick distanceToMove. Let's copy that.
02:11Let's paste that as well, and then we want to add a string literal.
02:15So we'll put a Plus sign, two tick marks for a string literal, semicolon and
02:20inside, we're going to put px.
02:22That way, the new photo position is going to have a CSS property value.
02:27Now, with our variable setup, let's hit few line returns and let's perform
02:30the animation, dollar sign, parentheses, tick marks for a string literal, .marquee_photos.
02:39We're going to grab the outermost container that holds all of the photos.
02:43Outside of the parentheses, .animate();.
02:50Inside of the animate, let's put two brackets.
02:53Inside of the brackets, we'll type left:
02:55and let's come up here and grab newPhotoPosition variable, copy to the
03:00clipboard, paste it inside of the brackets, outside of the brackets, comma, 1000
03:07milliseconds for 1 second.
03:08Now, we've set up our four variables and we're performing the animation, and
03:13again all of this is still inside of the click function.
03:15Let's come back to our browser.
03:17Let's hit Reload and now when I click on the individual dots, we'll see the
03:21photography also animates as well.
03:25So now that the photos properly animate to their corresponding spot, in the next
03:29movie, we'll work on the initialization settings for the marquee.
Collapse this transcript
6. Initializing Marquee on Page Load
Initializing the marquee
00:00Now at this point we want to set up our own custom function to initialize the marquee.
00:04This way when the page loads, we can do things like fade in the photos
00:07and preset the caption.
00:09So inside of our marquee.js file let's scroll all the way down to the bottom.
00:13Outside of our document ready area, we're going to create our first custom function.
00:17So we're going to type function space initializeMarquee, beginning and ending
00:28parenthesis, beginning and ending bracket, let's split that open.
00:31First thing what we want to do is go get the caption content, so dollar sign
00:36parenthesis, tick marks for string literals,
00:41.marquee_caption_content, .html outside of the parenthesis, beginning and
00:52ending parenthesis, semicolon. Inside of that html let's hit return,
00:57let's split that open, dollar sign, parenthesis, tick marks for string
01:02literal, marquee_panels .marquee_panel, then after we look for this
01:13particular class we're going to hit a colon, type first, then a space
01:21.marquee_panel_caption, outside of the parenthesis, .html, beginning and
01:30ending parenthesis.
01:31So what this statement is going to do is it's going to look for the very first
01:34marquee panel, pick out all-of-the html that's part of the marquee panel
01:38caption, and it's going to put it as the html value of the marquee caption
01:43content, which is in our marquee container.
01:45I'm going to scroll up a little bit. The next instruction inside of initialized
01:50marquee function, let's hit to return, dollar sign, parenthesis, tick
01:55marks for string literal, marquee_nav a.marquee_nav_item, colon,
02:11first, outside of the parenthesis. addClass, beginning and ending parenthesis,
02:20semicolon, inside of the parenthesis, for addClass tick marks for
02:24string literal, we're going to add the selected class.
02:28Next line, dollar sign, parenthesis, tick marks for string literal,
02:37.marquee_photos.fadeIn, beginning and ending parenthesis, semicolon,
02:49and then put 500 milliseconds inside.
02:51So again when we initialize the marquee, we're going to pick out the first
02:54caption, put that HTML content into the caption and content area.
02:57We're going to put the selected state on the first navigation item, and then
03:00we're going to fade in the photos.
03:02Now to make sure that this functions runs, let's come up here and copy the
03:05initialized marquee function, copy the name with the parenthesis and we're going
03:10to put this after the navigation item inside of the document ready.
03:13So let's paste that.
03:16Now the other thing we need to do is make a modification in the CSS, since the
03:20photography is already showing, we're going to need to hide that first.
03:24So let's come over and open the marquee.css and for the rule called marquee photos.
03:31Let's come into this rule and lets add display, colon, none, semicolon.
03:40Let's hit File Save.
03:41Make sure all of our files are saved.
03:42Let's move over to our browser and test this.
03:44And now in our browser we'll hit Reload and then you'll see the
03:49photography slowly fade in.
03:51So now we also have the caption updated but we haven't actually animated the
03:54caption to its new state.
03:55So in the next movie, we'll work on animating the caption.
Collapse this transcript
Setting the caption text
00:00Now to create the animation states for our captions, we're going to create a
00:03custom function for those.
00:05So after initialize marquee in the marquee.js file, let's type function,
00:14setCaption, beginning and ending parenthesis, beginning and ending bracket.
00:18Let's split that open on the brackets.
00:20So with the caption we need to set a few variables as well.
00:23Let's start by creating our first variable, var captionHeight, we're going to
00:32set that equal to, dollar sign, parenthesis, tick marks for string
00:36literal, .marqueeCaption, outside of the parenthesis .height, beginning and
00:47ending parenthesis, semicolon.
00:48Next line, other variable marqueeHeight, we're going to set that equal to,
01:00dollar sign, parenthesis, tick marks for string literal,
01:03.marqueeContainer outside of the parenthesis .height beginning and ending
01:15parenthesis, semicolon.
01:17Next variable is going to be var newCaptionTop.
01:19We're going to set that equal to, let's come up here and copy the
01:29marqueeHeight variable name.
01:31Let's paste that, space, then we're going to subtract the captionHeight.
01:37Let's copy that variable name, let's paste that and then let's subtract an
01:43additional 15, then a semicolon.
01:45This will make sure that the text inside of the caption is 15 pixels away from
01:49the bottom of the marqueeContainer.
01:50Now that we have these variables defined, let's type dollar sign,
01:54parenthesis, tick marks for string literal, .marquee_caption.
02:02That's out of the parenthesis .delay, beginning and ending parenthesis.
02:09Inside of the parenthesis for delay let's type 100 milliseconds or one-tenth of
02:13a second, then outside of there we're going to type .animate, beginning and
02:20ending parenthesis, semicolon.
02:22Inside of the animate parenthesis, two brackets, top, colon.
02:29Let's come up here and grab the variable name newCaptionTop.
02:33Let's paste that in here.
02:34So inside of the brackets it'd top, colon, newCaptionTop, outside of the
02:39brackets but still inside of the parenthesis, comma, 500 milliseconds.
02:44So basically what this script is doing is it's calculating the caption height,
02:48the overall marqueeHeight and we're finding out the new height for the caption
02:52based on the HTML content that's been injected in here and then once we know
02:56what that new caption height is, we're going to animate it after waiting
02:59one-tenth of the second to the new position.
03:01Now to make sure that this runs, let's come up here and grab the
03:05setCaption function name.
03:07Let's copy that and let's paste that inside of the initialized marquee function,
03:11then add a semicolon.
03:14Make sure all of your files are saved and to test this let's come down to our browser.
03:18Let's hit Reload, and now when the initialization function runs, not only will
03:22the photography fade in but the caption will now animate to its new state.
03:26Now we are only running these setCaption functions on the initialization state.
03:30So if I click between the different panels, the animation state won't actually
03:33change for the caption.
03:35So in the next movie we'll hook in the Set Caption function and some additional
03:38instructions to get the captions to react to the navigation buttons as well.
Collapse this transcript
Changing the caption to match the photo
00:01Now to get the captions to pay attention to the navigation items let's come into
00:04the click event that we have actually assigned to all of the navigation items.
00:09So after the variable newPhotoPosition let's hit a Return and let's add another
00:14variable var newCaption.
00:21We're going to set this equal to dollar sign, parentheses, tick marks for string
00:25literal, .marquee_panel_caption. Outside of the parentheses we're going to type
00:37.get, beginning and ending parentheses, semicolon.
00:41Inside of get we're going to put in the variable navClicked.
00:43So I am going to come up here and copy that, let's paste it inside of
00:47the parentheses here.
00:48Now that we've got that variable defined let's come down here and then after the
00:53instruction to animate the photography, let's add a few line returns, dollar
00:58sign, parentheses, tick marks for a string literal, type .marquee_caption.
01:07Outside of caption we're going to type .animate, beginning and ending
01:12parentheses, semicolon.
01:15Inside of animate we're going to put brackets, inside of the brackets we're
01:19going to type top, colon, two tick marks for a string literal, 340 pixels, outside
01:29of the brackets comma space 500 milliseconds.
01:33Then we're going to type comma space function, beginning and ending parentheses,
01:40beginning and ending bracket, let's split that open.
01:43So this is a callback function we're adding in here, so the instructions inside
01:46of this function won't run until after this animation has been completed.
01:50So now inside of the callback function we're going to declare a new variable var newHTML.
01:57We're going to set that equal to dollar sign, parentheses and let's go grab that
02:04
02:07variable newCaption. Just come up here and copy that variable.
02:10Let's paste that in here.
02:13So we're going to go find the actual caption that corresponds to the
02:17navigation that was clicked.
02:19Outside of the parentheses .html, beginning and ending parentheses, semicolon,
02:24then in the next line dollar sign, parentheses, tick marks for a string literal,
02:32.marquee_caption_content, outside of the parentheses .html, beginning and ending
02:43parentheses, semicolon.
02:44We want to set the HTML for the marquee_ caption_content equal to this variable
02:49here, which is newHTML.
02:51Let's copy that, paste it inside of the parentheses and then lastly type
03:02setCaption, parentheses and the semicolon.
03:06So basically what we're doing here is we're animating the top of the Animation
03:09panel all the way down to 340, so we can just see the very top, switching out
03:14all of the HTML and then animating it backup by calling the setCaption function.
03:21Let's make sure all of our files are saved.
03:23Let's go over to our browser and test this and let's hit Reload.
03:27Now when I click on the individual navigation dots the caption will move down,
03:31change the HTML content and then animate back up.
03:35The point at which it animates up is based on how much content you actually
03:38place inside of the caption area.
03:40Now with all of our functionality in place, in the next movie we'll add in the
03:43image pre-loader, so that the marquee doesn't actually load until all the images
03:47are available and we'll hide the actual data so that all we see is the marquee.
Collapse this transcript
Preloading images before initializing the marquee
00:00Now to add in the pre-loader we're going to go inside of the document ready and
00:03find where we ran the initialized marquee function.
00:07So let's come down here.
00:08Let's select this, let's cut it to the clipboard and in its place let's type
00:13dollar sign, parentheses, tick marks for string literal.
00:17Let's type .marquee_panels space img.
00:26Outside of the parentheses we're going to type .imgpreload, beginning and ending
00:33parentheses, semicolon.
00:35This is the custom function called for the plug-in.
00:38Inside of preload we're going to type function, beginning and ending
00:43parentheses, beginning and ending bracket.
00:46Let's split that open on the brackets and inside of here we're going to
00:49Paste initializeMarquee.
00:51So what this preload function is going to do is look for all of the images
00:54inside of the marquee panels container and once all of these images have been
00:59loaded, then we'll initialize the marquee.
01:01This will ensure that when users come to our site they won't actually see the
01:05marquee until all of the images have been loaded.
01:08Let's save our file, let's come back to marquee.css.
01:12The last thing we want to do is hide all of the individual panels, so that all
01:15we see is the marquee.
01:16So, let's come up to the marquee_panels rule.
01:22Let's add display none.
01:26Let's choose File > Save;
01:27make sure all of our files are saved.
01:28Let's go back to our browser, let's hit Reload one more time.
01:31Now running from your hard drive this is going to load immediately but if you
01:34put this up on your site you'll actually see a slight delay when the marquee
01:38loads, again based on how long it takes the images to load.
01:40We also don't see the data showing up anymore and now I have our fully
01:44functioning project.
01:47At this point we've completed the interactive marquee, in the next movie we'll
01:50talk about some places you might want to go from here and in addition you might
01:53want to check the Lynda.com Library for adding an autoplay to this particular
01:57project as a secondary course.
Collapse this transcript
7. Where to Go From Here
Where to go from here
00:00So now that we've completed designing and developing our marquee, there are a
00:04few things you might want to explore to add enhancements to this.
00:07On the jQuery site one of things you might want to do is, instead of having all
00:11of the data inside of the HTML page, perhaps we load that data in from separate
00:16HTML files or even from JSON or XML data.
00:19On the jQuery site to find documentation on this, go up to the Documentation tab
00:24and you're going to want to look under the Ajax settings and under Ajax the
00:31features you might want to use .load for example will gives you a really quick
00:35way to go out and pick an HTML file that's separate or even point to a script
00:39like a PHP or an ASP script or perhaps a developer can give you a link that
00:43feeds back the same HTML data.
00:45The other thing you might want to look at is putting a self-timer on the
00:49marquee, so that after a certain amount of time it will progress all by itself.
00:53On the Mozilla website, developer .mozilla.org, you can search for
00:57window.setInterval or just a regular set interval.
01:00This is a JavaScript looping command where JavaScript will just run a function
01:05after certain amount of time and so what you can do is set an interval on your
01:09marquee and every certain amount of time, maybe 10 or 15 seconds run a function
01:14that maybe sets up the captions and also progresses the photography as well.
01:18So I hope you enjoyed going through this course and I really appreciate
01:20you watching.
Collapse this transcript


Suggested courses to watch next:

Create Animated Rollovers with jQuery (29m 26s)
Chris Converse


Create an Interactive Map with jQuery (47m 38s)
Chris Converse

Set a Marquee to Autoplay with jQuery (36m 43s)
Chris Converse


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 98,695 instructional videos.

start free trial learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 1,899 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.


site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

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.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked