navigate site menu

Become a member and get unlimited access to every course in the library. Try it free for 7 days

Create a Tooltip with jQuery

Create a Tooltip with jQuery

with Chris Converse

 


Tooltips can greatly improve the usability of your web site by providing direction and cues to visitors. In this course, Chris Converse shows how to convert text or graphics into a mouse-sensitive tooltip. The tutorials demonstrate how to write the necessary HTML and activate the tooltip with jQuery, as well as add positioning and effects.
Topics include:
  • Saving web graphics from Photoshop
  • Creating the tooltip container
  • Adding the jQuery $(document).ready() and mouse events
  • Detecting and setting HTML-based tooltips
  • Attaching the tip container to the mouse
  • Setting the tooltip to fade in and out
  • Accounting for other CSS-positioned elements

show more

author
Chris Converse
subject
Developer, Web, Programming Languages, Projects, Web Development
software
jQuery
level
Intermediate
duration
54m 23s
released
Aug 28, 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 final project
00:03Hi! This is Chris Converse and this is a course on adding a jQuery based tooltip to your website.
00:08I want to start by first showing the final project of what we are going to be creating.
00:12So I am going to open the index.html file up in a browser.
00:15So when we roll over certain links inside of this document, you will see that we
00:19have a tooltip that fades in. When I scroll away, it fades out.
00:23When it's onscreen it also follows the mouse, and another interesting aspect is
00:27we actually position the tooltip right and left. So notice if I am on the
00:32left-hand side of the screen, the position of the tooltip is on the right.
00:35And if I roll over a tooltip that's on the right-hand side, we position it
00:39in toward the left.
00:40We are also going to be creating two types of tooltips, one that just have
00:44simple text and one that have HTML formatted content and graphics, like the one
00:47we are seeing here, and we are even going to detect for the height of the
00:51browser. So tooltips that are near the top of the screen, we'll position the
00:55tooltip underneath of the cursor, versus tooltips that will position
00:57themselves above the cursor.
00:59So to illustrate how we are going to do this, I want to bring up a sketch.
01:02So what we are going to be doing here is having HTML triggers on our page, and
01:06when our cursor rolls over one of the triggers, we are going to take a div, add
01:10content into it and then attach the div near the mouse, so as the mouse moves
01:15around, the tooltip will move around and then when we roll outside of the
01:18trigger, we are going to have the tooltip fade away.
01:21We're going to be writing this in such a way that the content for the tooltips
01:25is going to be in the HTML and on the trigger.
01:28So you can have as many tooltips on a page as you want.
01:31So I hope this course sounds interesting to you, and if so, let's get started.
Collapse this transcript
About the exercise files
00:00When you download the exercise files, there's going to be three folders inside of there;
00:04folders 1, 2 and 3.
00:05For those of you who are Premium members of the lynda.com online training
00:08library, you will have access to all three of these folders.
00:11However, for those of you who are not Premium members, we do offer all of the
00:15files inside of folder 1 for free.
00:17Now folder 1 contains all the files that are going to give us a basic layout
00:21that we can work from.
00:22Now you can follow along in this course by adding a tooltip into an existing
00:26website or web page that you have, or you can use the files inside of folder 1
00:30and work inside of here.
00:32For Premium members, we have folder 2 which contains a source Photoshop file
00:36with all of the slices intact.
00:37So you can use this to customize some of your graphics while you are taking
00:41this course, and then lastly, inside of folder 3 is a copy of the entire final project.
00:46So you can open up the files inside of here and see how the project that you are
00:50building is tracking with what we should have as a final project.
00:53So now that we are familiar with the exercise files, next we can work on
00:56creating the project folders.
Collapse this transcript
1. Creating Web Graphics
Creating a folder and adding the starter files
00:00Now to begin our project, let's create a new folder on the desktop.
00:06We'll name this myTooltip.
00:11I am going to just resize this a little bit and put it over here on the side.
00:19Once that folder is created, let's come back to the exercise files, let's open
00:23up folder 1, and let's grab a copy of the of the images directory, the includes
00:27directory and the index.html, and let's drag a copy of these over into that
00:31folder we just created on the desktop.
00:34So as I mentioned before, I have already gone ahead and created some of these files.
00:37I want to focus this course on creating the actual tooltip.
00:41So I want to give you a web page that you can actually work inside of, if you
00:44choose not to work inside of your existing website.
00:46And so what we are going to have here inside of the includes directory, is a copy of jQuery.
00:51I have two CSS files;
00:52the tooltip.css file has some rules in it, just for the layout.
00:56We are going to be adding our own in for the tooltip.
00:59The ie version of the CSS is empty.
01:01We are going to put our own CSS rules in there, and in the images directory, we
01:05have all of the graphics that the layout uses.
01:07So to take a quick peek at the layout, let's open up the index.html file up in a browser.
01:12So what we have here is a really simple, straightforward layout. We have a
01:15heading area across the top. We have a content area over on the left-hand side,
01:19and then a sidebar area over on the right-hand side.
01:22The design is liquid, so you can condense this down.
01:24We are actually going to be using this feature to show how we actually
01:28switch the tooltip from the right side to the left side, like we looked at
01:31in the introduction video.
01:32So now that we have a web page that we can put a tooltip into, next we'll work
01:36on customizing the graphics.
Collapse this transcript
Saving web graphics from Photoshop
00:00Now the exercise files that we're providing with this course, both the free
00:03ones and the Premium ones include an images directory which has all the graphics in here.
00:08So if you prefer to skip customizing the graphics in Photoshop, you can simply
00:12go to the next movie and just continue on with the course.
00:15For those of you who want to customize the graphics or at least see how the
00:18Photoshop file is setup, let's come over to the exercise files, let's open up Art
00:22Templates and let's open up template_items.psd.
00:26Now inside this Photoshop file here, I'm just going to zoom up here a little bit.
00:31Over in the Layers panel at the very bottom we have a Layer called fpo.
00:34We'll just turn that on. This is just simply a gray layer in the background, so
00:39that we can actually see through, some of these transparent graphics.
00:41So over on the toolbar we can click on the Cropping tool and see that we have
00:48the Slice and Slice Selection tool under the Crop tool.
00:51So I'll come down here and select the Slice Selection tool.
00:54This allows us to customize the individual slices.
00:57So down here I have a copy of the lynda.com logo that we're going to use inside of
01:01one of the tooltips. I just pulled this right off of the website.
01:05We have a logo file here. At the top we have a custom banner with a photograph
01:08inside, and then here we have a semi- transparent block that we're going to be using
01:13at the background for tooltip, since Internet Explorer 7 and 8 don't support
01:17semi-transparency in CSS3.
01:20To see how this is set up, I can come over here and select the layer called ie
01:24box and we can see that the Opacity is set to 85%. This is the same percentile
01:28we're going to set black to using CSS3 in the RGBA color space to get a 15%
01:33transparent black box.
01:35Now to customize the artwork inside of here, you can work on the canvas on
01:39any one these individual layers and realize that anything that's in the canvas
01:43that's inside of the slicing area is going to be included inside of that slice.
01:47Once you've completed that, come over here to the Layers panel, turn off the
01:52fpo layer, and what we're going to do now is export out all of these individual slices.
01:56So we are going to do that by coming up to the File menu, come down to Save for Web & Devices.
02:02Now if you're using Photoshop CS6 this menu will simply say Save for Web.
02:09Next we'll see the Save for Web dialog box. We can come in here and
02:13select individual slices.
02:14Over on the right-hand side we can pick the file format and the Optimization
02:17settings, so the banner is being set to a JPEG at 75% Quality.
02:23Now the logo was set to a 24-bit PNG, which is going to give us a lot of nice
02:28semi-transparency on the pixels.
02:29The lynda.com logo is being set as a GIF, and then this slice here that we're
02:35going to use as the background for IE 7 and 8 is set to a 24-bit PNG as well.
02:39Now all of these slices and all of the Optimization settings are saved inside
02:43of the PSD file. Let's come down here and click Save from the Save Optimized As
02:50dialog box, let's come to Slices. You can come down here and choose All User Slices.
02:55This way Photoshop will only pay attention to the slices that we have drawn and
02:59it will ignore the rest of the slices that are used to complete the canvas.
03:03And from the desktop, let's choose myTooltip > images directory, choose Save and
03:10when Photoshop asks you if it's okay to replace all these files you can come
03:13over here and click on Replace.
03:15And that will then customize all of these graphics with whatever changes you've
03:18made inside of your Photoshop file.
03:20Once you've exported your graphics, you can come up to the File menu, close your
03:24file, choose Save, and now that we have all of our graphics created, we can then
03:28work on editing the HTML and CSS files in the next movie.
Collapse this transcript
2. Adding HTML Markup
Creating the tooltip container
00:00At this point we're ready to open up our HTML file and start making some
00:03changes, so I'm going to open this up in the text editor.
00:07Now if you're interested in taking this course in a more visually oriented tool,
00:10we're offering the same course in the online training library using Adobe
00:14Dreamweaver inside of a text editor.
00:16And you can follow along in any text editor or any web editor that gives you
00:20access to the source code.
00:22Now for those of you who are following along and using this project file
00:25instead of your current website, I want to point out some of the things that
00:28I've already setup in this file.
00:30First is I've created a hook into the tooltip.css file, so we are actually
00:35linking to the CSS file that we're going to be modifying.
00:37I've also linked to a copy of jQuery. I've linked to the tooltip.js file.
00:42And down here we have an HTML conditional statement which is targeting browsers
00:46that are less than IE 9, so this is targeting IE 7 and 8, and we're doing two
00:50things here, we're linking to a copy of the HTML5 Google shiv from Google, which
00:56takes HTML 5 elements and allows IE 7 and 8 to understand what those are.
01:01And down here you can see we're using things like header, article, and aside.
01:06These are all HTML5 tags that IE 7 and 8 don't understand.
01:10So this Google shiv will enable IE 7 and 8 to understand that.
01:14And then we also have a link to a tooltip_ie.css.
01:18This is where we're going to put some custom rules for all of the things that IE
01:227 and 8 don't understand as part of CSS3.
01:25Now the first thing we need to add to our HTML file is a container that's going
01:28to hold a tooltip itself.
01:30So let's scroll down here in the code, let's find the line for the footer, let's
01:35hit a Return, so we are outside of all of our content-containers, but we're
01:39still inside of the main div, that's got a class of page.
01:45So inside here we're going to start by typing a div tag, space, we're going to set an id.
01:52The name of the id we're going to create is going to be tooltip_container, then
01:59let's end the div tag.
02:02So the reason we're using an id here is ids are meant to be used once on a
02:06page which differs from a class element, which can be used any number of times on your web page.
02:11So again, we're going to use an id here just to remind ourselves that this
02:14tooltip_container should only appear once in our page.
02:17And now to make this so that we can actually see it in the page to help us with
02:20some of our CSS rules, let's temporarily put some text into here.
02:26We'll just type this is my tooltip and then hit Save.
02:31Now that we've a container for our tooltip. Next, we can work on the CSS styles
02:35so that we can define the way the tooltip should look.
Collapse this transcript
Styling the tooltip
00:00Now to create a CSS rule to style our tip container, we're going to need to open
00:04the tooltip.css file.
00:06So from your text editor, choose File > Open, from the myTooltip folder let's go
00:13into includes and let's open tooltip.css.
00:19Now there are CSS rules in here which are controlling our layout and style, and
00:22down at the very bottom we have a comment for Tooltip.
00:25This is where we're going to add our own custom rules for the tooltip.
00:28So I'll just add a few lines here at the bottom and what we are going to want to
00:31do is target that tooltip_container.
00:33Now it is an id, so we target that with the pound sign, tooltip_container, then beginning and ending bracket.
00:45Let's split these brackets open.
00:47Now the first property we're going to set is going to be the color of the type,
00:52so we're going to set color: # fff; for white
00:57Next line, we're going to set position of absolute, this way the tooltip with
01:04absolute positioning can hover over the top of other objects and have its
01:08position changed without any relation to the other divs on the page.
01:11Next line, we're going to set padding of 20 pixels, that's going to set padding
01:17on the top right bottom and left.
01:19We're going to set a max-width of 200px, that way regardless of any content we
01:26put inside out there, the tooltip will never get wider than 200.
01:29And of course, you can customize this for other values.
01:32Next, we're going to set the background-color.
01:35Now we're going to use the RGBA color space.
01:38So we're going to type after background-color: rgba, beginning and ending parenthesis, then a semicolon.
01:46Inside of the parenthesis we're to specify the red, then the green, then the
01:49blue and then the alpha.
01:52So to get black with RGBA, we need to set the RGB values all to 0.
01:57So I will type a 0 for red, 0 for green, 0 for blue, then .85. This is going to give
02:05us 85% Opacity.
02:07Next we want to specify rounded corners, and we still need to use some specific
02:11rendering engine flags.
02:14The first is going to be webkit.
02:17This is going to target Chrome, Safari, Android and the iOS Platform.
02:21So -webkit-border-radius, colon, space. We are going to set this to 12px.
02:35Next, we're going to set the flag for the rendering engine that runs inside of
02:40Firefox, which is Mozilla.
02:42So I'm going to copy that entire line and then replace webkit with moz for Mozilla.
02:50Then on the next line we're going to add the straight CSS3 property, which
02:54actually is supported in IE 9, and other browsers are starting to support the
02:58straight CSS property instead of using rendering engine flags.
03:04So we'll type border-radius: 12px.
03:09Once these properties are added, let's choose File > Save, and now let's open up
03:13the Internet Explorer version of the CSS file.
03:17So in the code editor, let's choose File > Open.
03:20Let's open tooltip_ie.css.
03:24So inside of here we're going to target to tooltip_containers as well,
03:27#tooltip_container, beginning and ending bracket, split that open.
03:39Now IE 7 and 8 don't support the maximum width setting, so we're going to come
03:44in here and simply set a width of 200px.
03:47On the next line we're going to set background, colon, space, then we are going to type none with a semicolon.
03:56So here we want to clear out all of the background settings, since IE 7 and
04:008 don't understand the RGBA setting for background-color.
04:03So we'll just set background to none.
04:06Then on this line, we're going to specify a background image and use that
04:08transparent PNG, so we're going to type background-image: url(../images/ie_transparency.png);.
04:37So again, we're setting the width, because IE 7 and 8 don't understand
04:40max-width. We're clearing out the background to get rid of the background color
04:43we specified in the tooltip.css, and then the background image we're bringing in
04:48with the semi-transparent PNG.
04:50That's going to gives us the same effect as the CSS3 is giving us in the other browsers.
04:54Now again, if we go back to the index.html file and then scroll to the top,
04:58remember we're only bringing in this CSS rule for IE 7 and 8.
05:02So other browsers are going to completely ignore everything on line 9 through 12.
05:08They are going ignore the Google Shiv, they are going to ignore the tooltip.css;
05:11only IE 7 and 8 are going to see this.
05:14So now with our CSS rules in place, let's come back to our project folder, let's
05:18open up the HTML file up in a browser, let's hit Reload, and we should see the
05:23This is my tooltip in the lower left-hand corner here.
05:25We see our rounded corners. We see that the background is 85% opaque against
05:31the blue background, and we see that the container is actually showing up after
05:34the footer element.
05:35Now even though we did specify absolute positioning on this object, we haven't
05:39given it a top or a left property yet.
05:41So we haven't actually told it to position itself anywhere. We're going to be
05:45doing that later on with JavaScript.
Collapse this transcript
Adding the tip info
00:00Now in order for us to be able to have content show up in the tooltip, we need
00:04to put some data into the HTML file that we can actually pull from.
00:08So in the HTML file, we are going to start by adding some of that data into the
00:12anchor link that's inside of the article, now not inside of the aside, but
00:16inside of the first paragraph, inside of the article.
00:18And the linked text says text-based tooltip.
00:22So this anchor link here, right now we are pointing to a pound sign which
00:25is just a self link.
00:27Now inside of here we want to add some additional attributes.
00:29So we are going to start by adding some data attributes, which is fully
00:33compliant with HTML5 and is the recommended way for putting data onto an HTML
00:39element that's not seen by the public, but gives us the ability to access
00:43that through JavaScript.
00:44So inside of the anchor tag, after the href attribute, let's hit a space and
00:49the first data item we are going to put in here is going to be data-tip-type, equals, two quotes.
01:01We are going to put in the word text. Now we are making up this value, we are
01:05going to have two different types, if you remember from the Introduction movie, a
01:08text-based tooltip and an html-based tooltip.
01:11So after that attribute, but still inside of the anchor tag, let's hit a space.
01:15We are going to type data-tip-source, equals, two quotes, and in here we are going to
01:27put the actual text that's going to show up as the tooltip text.
01:32So we'll type "This is text for a tooltip."
01:37And the last thing we are going to add to this anchor tag is going to be a class;
01:42this is how we are going to actually target these items with jQuery.
01:45So lastly we'll add a class attribute, equals, two quotes, and the class is going to be tooltip.
01:51Then hit Save.
01:55Now we are using a class here because we want to reuse the name tooltip, any
01:58number of times on our page, and what's really advantageous about this is we
02:02can write one jQuery statement that targets tooltips, and then we are going to
02:06come in here and check to see what type of tooltip it is, and then what the actual content is.
02:12So now that we have defined an HTML element as having a class of tooltip and
02:16having a data tip type, and a data tip source, next we can work on actually
02:20finding this object and then modifying the tooltip container based on the data
02:24inside of this element.
Collapse this transcript
3. Styling and Activating the Tooltip
Adding the jQuery document ready and mouse events
00:00Now at this point we're ready to start writing some JavaScript.
00:03So let's open up the JavaScript file. From your text editor, go to File > Open.
00:07From the myTooltip folder, let's go into includes, and let's open up tooltip.js.
00:15So inside of here, we are going to write some jQuery to target that tooltip and
00:19then change the tooltip container content.
00:21So let's start by putting in jQuery's document ready, so that's $(), type in the
00:29word document inside of the parentheses, outside of the parentheses
00:32we are going to type .ready();
00:39inside of the parentheses for ready, we are going to type function, another set
00:44of parentheses, beginning and ending bracket. Let's split the brackets open.
00:50So this is jQuery's document ready structure.
00:53So these instructions won't run until the HTML file has been loaded.
00:57So inside of the document ready, the first thing we want to do once the HTML has
01:01been loaded is target the tooltip and set the mouse events.
01:04So I am going to start with a $(), two tick marks inside for a string literal,
01:10we are going to search for .tooltip. This is going to target every HTML element
01:16in our page that has a tooltip, so we can have more than one on the page.
01:20Outside of the parentheses .mouseover, beginning and ending parentheses, outside of the parentheses we are
01:28going to type .mousemove, another set of parentheses, outside of those
01:34parentheses .mouseout, then a semicolon.
01:43So we are attaching three properties onto the tooltip, mouseover, mousemove and mouseout.
01:48Inside of each one of these parentheses, we're going to actually target the
01:52mouse event itself. So inside of each one of these parentheses, we're going to
01:55put a custom function that targets the actual event.
01:58So we are going to type function(), inside of the parentheses, we are going to
02:05put letter e, outside of the parentheses, but still inside of the parentheses
02:10for mouseover, we are going to put beginning and ending bracket.
02:16I am going to select this first one here, let's come over here and paste this
02:21inside of each of the parentheses for mousemove and mouseout.
02:25Once we have all of these in place, let's come over here and just split these
02:28open, split each one open on the brackets, just close up some negative space.
02:39So now we have an area to put instructions in for mouseover, for mousemove and
02:43mouseout, and we'll start that in the next movie.
Collapse this transcript
Detecting and setting text-based tooltips
00:00So the first thing we want to do in the mouseover event is to detect what type
00:04of tooltip we're actually looking at.
00:06So we're going to open this up a little bit inside of mouseover and we're
00:10going to start by typing an if statement, so we're going to type if,
00:13beginning and ending parentheses, then beginning and ending bracket, let's
00:17split the brackets open.
00:20Let's come up inside of the parentheses and what we're going to do is test the
00:23actual item that was rolled over.
00:25So since we're inside of whatever tooltip was triggered, we can come in here and
00:29type dollar sign, parentheses, and the word this inside of the parentheses,
00:35that's going to return whatever item on the HTML has a tooltip class and was
00:39actually rolled over by somebody's mouse.
00:42So for that object, after this, we're going to type .attr for attribute, put in
00:49our parentheses, two tick marks inside for string literal. We want to look at
00:53the actual attribute called data-tip-type, outside of the parentheses hit Space,
01:02two equal signs to test the absolute equality, then a space, two tick marks
01:07for a string literal, and then type in the word text.
01:12So what we're doing is any item that's rolled over with a tooltip class,
01:15we're going to check the attribute of that object and see if the
01:18data-tip-type was text.
01:20If it is, we're going to put our instructions inside of this if statement.
01:23So what we're going to do here is we're going to target the tooltip container,
01:27dollar sign, parentheses, tick marks inside for string literal, pound sign,
01:33because the tooltip container is an ID, tooltip_container, outside of the
01:41parentheses .html, parentheses, then a semicolon.
01:48Now the value of the HTML is going to be the attribute of the same object we
01:53rolled over called data-tip-source.
01:56So inside of the parentheses for HTML, let's start with dollar sign, parentheses,
02:02this, outside of the parentheses, attr for attribute, inside of the parentheses,
02:08two tick marks for string literal, data-tip-source.
02:16Now to test this let's choose File > Save, let's come back out to our project
02:20files, let's reload this in a browser and then when I come over here and
02:24rollover the text based tooltip, once I rollover, you'll see that the text
02:28actually changed down here to this is a text for tooltip.
02:33To see that again, I'll hit Reload.
02:35We have This is my tooltip, which is the default text we put in.
02:39Come over to the rollover and then it changes to this a text for tooltip.
02:43So now we're properly targeting every item that has the class of tooltip and
02:47we're able to pick out the data to see what type it is and pull out the source
02:51to populate the container with that particular data.
02:55So now that we have text-type-tooltips ready.
02:58In the next movie, we'll start building our structure for the HTML
03:01based tooltips.
Collapse this transcript
Creating HTML containers
00:01Now back in our HTML file, we need to have a place inside of our file where we
00:05can store HTML data for a tooltip.
00:08Now inside of our data-tip-source attribute which I'm highlighting right here,
00:12we can put text based information, but we can't put HTML content in here.
00:17So if we want to format our tip in any way, maybe change the color, use bold
00:22type, bring in images, we've to do this with HTML, and we can't put HTML inside
00:27of an attribute on an HTML element itself.
00:31So what we're going to do is create a container to hold all of the HTML data
00:35that we want to be able to pull from for tips that have HTML content.
00:39So inside of our HTML page, let's come down to after the tooltip container,
00:44let's hit Return, so let's add a div, let's give this a class and let's call it
00:51tooltip-html-source.
01:00Let's end that div tag. Let's split that open and inside of this div container,
01:06we're going to put additional div containers for each tooltip, and this is
01:10where we're going to store the HTML data, so we can populate that into the tooltip container.
01:16So inside of here, let's start with a div tag, just hit a space, we're going to
01:21set id equal to two quotes. Inside we're going to call this
01:26tooltip-sidebar, let's end that div, let's split that open.
01:36Now inside here we want to add the HTML for that tooltip that has the
01:39lynda.com logo in there.
01:42So I'll start with an image tag, we will set a source images/lynda.gif, space,
01:55style equals two quotes. We'll just put an inline style on this.
02:02We'll set that to float:left, outside of the attribute forward slash and
02:07then end the image tag. Next we'll type;
02:13This is a tip that has.
02:17Next we'll add an HTML tag to make the next words bold, so add a strong tag.
02:25Next, we'll also make the words italic, so we'll type em for emphasis, let's add
02:31a space, let's add an attribute called style, equals two quotes. We're going to
02:38change the color of the bold and italic text, color, space, pound sign ffe19a,
02:47that's going to give us that light yellow color, then a semicolon.
02:50So this is an inline style on the emphasis tag, let's end that tag. We'll type
02:55HTML-formatted, let's end the emphasis tag, then we'll end the strong tag, then
03:05a space, and the word content, comma, space, including an image and a period.
03:16So inside of this div with an id of tooltip-sidebar, we're bringing in an image,
03:21we're writing a sentence that says this tip has HTML formatted content including an
03:25image, and the word HTML formatted is going to be strong and italic.
03:31Now we're floating this image up here, so we want to make sure that the
03:34tooltip container can expand to encompass all of its children that have float properties.
03:39So let's hit Return, let's type div. We will set a class and I have a CSS rule
03:45for clearing all the objects, so we'll type clear-all, and then end that div
03:53tag. Let's save our work.
03:56So now we have all of the HTML in place that we're going to put inside of the
03:59tooltip container. We also created the tooltip HTML source container.
04:03Let's come up here and copy that class name, let's switch over to tooltip.css,
04:09and let's come in here and hide that. We don't actually want to see the
04:12container that's holding all of the HTML content.
04:15So let's type a period, let's hit paste, so .tooltip-html-source, beginning and
04:22ending bracket. We're going to set display to none.
04:27That way when this loads, all of the HTML content inside of the HTML source
04:31is going to be hidden.
04:33Now that we have the HTML in place to hold the content for an HTML tip, in the
04:37next movie we can add attributes to our aside anchor link, and then do a
04:41detection in jQuery to test for HTML-base tips.
Collapse this transcript
Detecting and setting HTML-based tooltips
00:00Now back in our HTML file, what we need to do here is add in those data
00:04attributes into the anchor link that's inside of the aside container.
00:08So let's scroll up here, let's find the aside container right here, inside there
00:12is an anchor link with a HTML-based tooltip link here.
00:15Let's come inside of the anchor tag, after the href attribute, hit space, we'll
00:21type data-tip-type, equals, two quotes.
00:28Inside of the quotes we'll put html, then we'll add another attribute, hit a
00:33space, data-tip-source, equals, two quotes, and instead of putting actual text
00:42here, what we're going to do is point to the id of the container we want to use.
00:47So if I scroll down here, we created an ID down here called tooltip-sidebar.
00:53Let's select that name, copy to the clipboard, let's come up here and
00:57let's paste that in here.
01:00And then finally, let's come in here and add a class of tooltip.
01:05Let's choose Save, let's switchover to our JavaScript file.
01:08Now inside of the mouseover, we have our first detection here that's checking the
01:13data-tip-type and testing to see if it equals text. Let's copy this entire
01:18statement, let's hit few a Returns, still inside of the mouseover area, let's hit
01:23Paste and let's come in here and change text to html.
01:30So the second if statement is testing the actual item that's being rolled over
01:33to see if the attribute data-tip-type is set to HTML, and if it is, we're going
01:37to run these instructions inside of here.
01:39So let's get our cursor in front of the first statement, let's hit two Returns,
01:43and above that what we're going to do is declare a new variable, we're going to
01:47type var elementToGet. We're going to set this equal to, two tick marks for
01:56string literal and a pound sign.
01:59Outside of the string literal, we're going to type a plus sign and then we're
02:03going to type dollar sign, parentheses, this, outside of the parentheses .attr,
02:11beginning and ending parentheses, semicolon.
02:14Inside of the parentheses, two tick marks for string literal, and we want to go
02:17out and get the data-tip-source.
02:25So what this does is it goes out to the item that's being rolled over and we're
02:28checking the attribute data-tip-source, which in this case is actually the name
02:32of the ID where we put all of the HTML we want to have injected. We're adding a hash
02:37symbol here and putting these together or concatenating this into this single
02:42URL. It is going to give us something similar to what we're doing up here, where
02:47we're using jQuery to find a specific ID.
02:49So once we've defined our variable elementToGet, on the next line we're going to
02:53declare a second variable. We're going to call this one newHTML and we're going
02:59to set that equal to, and then we're going to type dollar sign, beginning and
03:04ending parentheses, and we want to get this element, so let's copy that variable
03:09name, let's paste it down here, outside of the parentheses we're going to type
03:15.html, beginning and ending parentheses, then a semicolon.
03:19So we're going to go get whatever ID is specified in that link, we're going to
03:24pull all the HTML into the newHTML variable, and then on this line we're going
03:28to keep the tooltip container HTML, but instead of setting it to the value of
03:32the data-tip-source, let's remove that, and let's come in here and copy the
03:37newHTML variable, and let's paste that in there instead.
03:42Let's save our document.
03:43Let's come back out to the browser and let's say Reload.
03:47Now over in our browser, we can rollover the HTML base tooltip, and then we can
03:51see the contents of the tooltip are now changing to all of the HTML that we put
03:54inside of that ID. Then I can go back to the text-based one and see just the
03:58text that is actually being pulled from that source attribute.
04:01So now that we are properly querying every one of the tooltip items and
04:04detecting whether they are text or HTML and populating the tooltip container,
04:08in the next movie we can now start to work on attaching the tooltip to where
04:12the mouse is.
Collapse this transcript
4. Adding Positioning and Effects to the Tooltip
Attaching the tip container to the mouse
00:00Now we need to work on actually moving the tooltip container to match where
00:04the user's mouse is.
00:05So we're going to come inside of the mousemove area here, let's add a few
00:09Returns, and we want to start by declaring a variable.
00:11We're going to say var, space, toolTipWidth, we're going to set that equal to
00:19and we want to figure out how wide the actual tooltip container is.
00:23So dollar sign, parentheses, two tick marks for string literal, #tooltip_container,
00:32outside of the parentheses we're going to type .outerWidth();.
00:45Next line, second variable, toolTipHeight = $(), tick marks for string literal,
00:57pound sign ('#tooltip_container').outerHeight.
01:11Now we've set a maximum-width of 200 pixels but the height can actually vary
01:16based on the HTML data that we're pulling in here or the text data that we're
01:21pulling in up here, and so depending on how much content goes in there, the
01:24actual size of that tooltip container is going to change.
01:27So we want to declare the height and the width and variables so that we
01:30can refer back to them.
01:32So after the variables, let's start with $(), tick marks inside for a string literal.
01:38We're going to target tooltip container again.
01:44Outside of the parentheses we're going to type .css();.
01:52Inside of the parentheses we're going to start with the string literal, two tick
01:55marks, we're going to set the left property outside of the string literal but
02:00still inside of the parentheses.
02:02We're going to type a comma, another set of parentheses.
02:05We want to do a calculation inside of here.
02:07We're going to type e.pageX.
02:12Now e right here is the event that's triggering this, which up here is the
02:16mousemove, so when we run this generic function, e refers back to the mouse.
02:21So e here is the mouse movement and then pageX is a JavaScript statement that's
02:26going to tell the browser to let us know where exactly the user's cursor is.
02:30So we're going to take the x position and then we're going to subtract 20 from it.
02:35That way the tooltip will be a little bit to the right of where the actual x
02:39value is, of where the cursor hits the tooltip.
02:42Now outside of the parentheses, plus sign, two tick marks for string literal, and then px.
02:48So we're taking whatever the x value is, subtracting 20 pixels and making this
02:52whole statement here a CSS property with the px on it, so that's going to be
02:57the new left property.
02:58Let's hit a Return. Let's come up here and copy this whole line, let's paste it
03:05on the next line, let's come down here and instead of left, we're going to set
03:09the 'top' value to e.pageY, and then we're going to add 20 pixels to this.
03:17Now all of this is happening inside of the mousemove function up here, so
03:21let's save our file, let's come back out to the browser. Let's hit Reload, and
03:26now when we move over tooltip, we'll see that the position of that tooltip now
03:30sticks near the cursor and as we move the mouse, we constantly update the
03:34position of that object as well. Same thing for the HTML-based tooltip on the right-hand side.
03:39Now one thing you'll notice when I rollover the tooltip on the right-hand side,
03:44the tooltip, since it's aligning itself to the left and positioning to the
03:47right, actually moves off the browser page.
03:50So in the next movie we're going to work on detecting where the tooltips are and
03:54if they are on the right-hand side of the screen, we're going to position the
03:58tooltip toward the left.
03:58So the tooltips will always position themselves, so that they're visible to the user.
Collapse this transcript
Positioning the tooltip to the right or left of the cursor
00:00So the first detection we're going to do is for the pageWidth, so that we know
00:04whether we need to align the tooltip to the right or left, depending on where
00:08the mouse cursor is.
00:09So after the variables for tooltip width and height, let's come in here and
00:12declare another variable, var, space, pageWidth.
00:18We're going to set that equal to = $(), tick marks for string literal 'body',
00:26outside of the parentheses .width(); so the width of the overall browser is now
00:34going to be stored in this pageWidth variable.
00:36Next line, let's type a JavaScript conditional statement, if, beginning and
00:42ending parentheses, beginning and ending bracket, open up the brackets.
00:47Inside of the parentheses we want to detect the position x of the cursor so
00:52e.pageX > pageWidth/2. So if pageX is greater than pageWidth/2, we know that the
01:05cursor is on the right-hand side of the screen.
01:08So what we want to do inside of here is type $() tick marks for string literal,
01:13pound sign ('#tooltip_container').css();.
01:26Inside of the parentheses for CSS, two tick marks for string literal, then set
01:30the 'left' property. Outside of the string literal, but still inside of the
01:34parentheses for CSS,
01:35I'm going to type comma, another set of parentheses e.pageX-
01:44toolTipWidth, let's come up here and copy that variable name.
01:48Let's paste it inside of there and then plus 20 pixels, outsides of the parentheses,
01:57plus sign, two tick marks for string literal and then px.
02:03So what this is going to do is take the toolTipWidth that we calculated earlier
02:07and subtract that from the pageX position, so that the tooltip will always be
02:11offset from the cursor, based on the width of the actual tooltip itself.
02:15Let's come down to the bracket for the conditional statement.
02:19Let's add an else statement with brackets, and then let's come down here and
02:23let's grab the rule we have down here.
02:27Let's cut that and paste it inside of the else statement.
02:31So if the pageX is less than the pageWidth divided by 2, so we know on the
02:36left-hand side of the screen, we'll continue to leave the tooltip_container
02:39positioned to the right, which is what we had before we did the width detection.
02:44Let's hit Save, let's come back to our browser, let's hit Reload.
02:50So now in our browser, I'm going to come up here and rollover a tooltip that's on
02:53the left-hand side of the screen, and we can see that the tooltip aligns to the
02:57left and goes further over into the right-hand side, and if I rollover the
03:01tooltip on the right-hand side, the tooltip aligns to the right and more of the
03:04content shows over in the left-hand side.
03:07And this is dynamic as well, so if I come down here and collapse this down so
03:11that we have the text over here, wrapping around the columns, this side will
03:16align over to the left and this side will align over to the right.
03:21So now that we're properly detecting for right and left, in the next movie
03:24we're going to add a tooltip into the logo and we're going to detect for height as well.
Collapse this transcript
Positioning the tooltip above or below the cursor
00:00So now we're going to add in detection for the pageHeight. This is going to
00:04allow us to do two things;
00:05one, we can put the tooltip_ container above the cursor,
00:08and two, we can make sure that the tooltip doesn't touch the top of the browser.
00:11Now in order to get a link into the top of our page let's come back to
00:15our index.html file.
00:17In the heading area, we have an anchor link with the class of logo, let's come in
00:21here and add our tooltip properties to this link.
00:24First, let's add a second class onto here, so after logo inside of the value for
00:28class, let's hit a space, let's add in tooltip.
00:34Now after the attribute for href, let's add in data-tip-type, equals, two quotes, put in "text".
00:47Let's add one more attribute, data-tip- source, equals, two quotes, and I'll just type in
01:00here, "This is the logo".
01:03Let's hit Save, now the logo will act as a tooltip as well, let's come to back
01:08to our tooltip.js file.
01:10Now inside of here, inside of mousemove, after we do the pageWidth detection,
01:16let's hit a few Returns and what we're going to do in here is but a standard
01:20JavaScript conditional statement.
01:21So if () and then beginning and ending bracket, split that open on the brackets.
01:29Inside of the if statement what we want to do is test for the pageY, so
01:32e.pageY, and if that is greater than 100, and we're going to test to see
01:40whether this is greater than or lower than 100 pixels, because in the CSS we've
01:45set the header area to take 100 pixels, so we'll know if the cursor is below
01:49the 100 pixel mark.
01:51And if it is, what we're going to do inside up here is target the
01:55tooltip_container, so $() tick marks for string literal, a pound sign
02:03('#tooltip_container'), outside of the parentheses we're going to type .css ()
02:11then a semicolon, inside of the parentheses another string literal.
02:16We're going to type 'top,' outside of the string literal hit comma, another set
02:22of parentheses, outside of the parentheses plus another string literal 'px'.
02:29Now inside of the parentheses, e.pageY-, then another set of
02:37parentheses, now inside of the innermost parentheses, let's come up here and
02:41let's copy toolTipHeight, copy that variable name, let's come down here, let's
02:46paste that and then put +20.
02:48So what we're doing here is taking the height of the tooltip, adding 20 pixels
02:53and then subtracting that from the position of the cursor.
02:56Now what this is going to do is position the tooltip above where the cursor
03:00intersects with any HTML element with the tooltip class.
03:04Now let's add an else onto this condition, so after the bracket type else,
03:09another set of brackets.
03:11Let's come down here and let's take the css 'top' property we were using before.
03:17Let's cut that to the clipboard, let's paste it into here.
03:22Now to test this let's choose Save, let's come back up to the browser and hit
03:26Reload. Now you'll see our tooltips are now going above the cursor.
03:32Now while I come up here and rollover the logo, notice that the tooltip is now
03:36activated for the logo, but the position of the tooltip is now showing up
03:40underneath the position of the cursor.
03:42So now that we're properly detecting for height and width position, in the
03:45next movie we'll hide the tooltip, so that it only shows when somebody rolls
03:49over a tooltip item.
Collapse this transcript
Hiding the tooltip container until it's triggered by the mouse
00:00Now to hide the tooltip, we are first going to set a CSS property.
00:03Let's come back to tooltip.css.
00:06Under the id for tooltip_container, after border-radius, let's hit a Return.
00:12Let's set a property of display, set that equal to none, choose Save, let's come
00:18back to tooltip.js, let's scroll up and let's look for the mouseover function.
00:25Inside up here, after we do our two detections, one for the data-tip-type and
00:29one for the source, let's add in an instruction to target the tooltip container,
00:34dollar sign, parentheses, tick marks for string literal, pound sign,
00:40tooltip_container, outside of the parentheses .css, parentheses, semicolon,
00:50inside of the parentheses two tick marks for a string literal.
00:54First property we are going to set is display.
00:57Outside of the string literal, comma, two tick marks for the value, we are going
01:01to set this to 'block.' That's going to turn that from being a display none,
01:05into an object that has a display type.
01:07Now that's in the mouseover, let's scroll down to the mouseout, and inside of
01:14the mouseout function, let's target the tooltip_container again, dollar sign,
01:19parentheses, string literal, pound sign, tooltip_container, outside of the
01:28parentheses .css, beginning and ending parentheses, inside two tick marks, set
01:37display, second string literal. We are going to set that back to none.
01:43Then we are also going to set one more property here.
01:46After the parentheses for css, we are going to set .html, another set of
01:51parentheses and a semicolon, two tick marks inside for a string literal.
01:56So in addition to setting the display to none, we want to wipe out all of
02:00the HTML that we have put inside of that tooltip container. So that when we
02:04roll out, everything inside is going to be set to null, and then we'll set
02:08the display to none.
02:09To test this, let's choose Save, let's come back out to the operating system,
02:13let's reload this in the browser. Notice we don't see the tooltip in the lower
02:17left-hand corner here, and then I roll over my tooltips, they pop up, and when I
02:22move away, they disappear completely.
02:25So now that our tooltip is properly functioning, next we'll take a look at
02:28adding an optional fade in effect, where we can fade the tooltip in and out
02:32based on whether a user rolls over a tooltip item.
Collapse this transcript
Setting the tooltip to fade in and out
00:00Next we're going to add a fading effect, so when we rollover an HTML element
00:04with our tooltip class, we're going to fade the tooltip in, then allow it to
00:08move with the mouse and then fade it out on mouseout.
00:12So to add in this behavior, let's come up to the mouseover function inside of
00:16the JavaScript file, let's come down to where we're setting tooltip_container
00:20display to 'block' on mouseover, and what we're going to do in here is set an
00:25additional CSS property.
00:26Now to set more then one CSS property, we need to put the properties inside a
00:30set of brackets, inside of the parentheses for CSS.
00:33So after the beginning parentheses, let's put in a beginning bracket, get your
00:38cursor before the ending parentheses, and put in an ending bracket, and then
00:42we'll come back and change that comma to a colon.
00:44So the first property we're setting is display, let's hit a comma, two more
00:50tick marks for another string literal, let's put opacity, outside of the
00:55string literal, colon 0.
00:58So what we're doing here is in addition to setting the display to 'block,' so
01:02that it actually shows up in the page, we're setting the transparency to 0, so
01:06we can't see it yet.
01:07Then outside of the parentheses for CSS and before the semicolon, we're going to
01:11add another jQuery statement, .animate, beginning and ending parentheses. Inside
01:19of the parentheses we'll add a beginning and ending bracket.
01:22Inside of the brackets we want to animate the opacity setting, so we'll type
01:26opacity:1, then outside of the brackets but still inside of the parentheses,
01:32comma 250 milliseconds.
01:36So on mouseover we're going to set the display to block, set the opacity to 0,
01:40and then animate the opacity up to 1 or 100% over the course of one quarter of the second.
01:45Now to fade our tooltip back out, let's scroll down in our JavaScript file,
01:50let's find the mouseout function, let's add a few returns in here.
01:56So this particular statement here tooltip container that sets the display to
01:59none, once we set the display to none, the tooltip is just going to pop off the
02:04screen. It's not going to actually animate down, so we're going to need to
02:07animate the opacity back down to 0 and then set the display to 'none.'
02:11So to do this let's add few Returns above the tooltip_container. Let's start by
02:17targeting the tooltip_container again, so dollar sign, parentheses, tick marks for
02:21string literal, pound sign, tooltip_ container, outside of the parentheses
02:29.animate. Now let's add a parentheses, then a semicolon inside of animate,
02:37beginning and ending bracket, opacity:0, outside of the brackets, comma 250 for
02:46250 milliseconds. Then we're going to hit another comma, type in the word
02:52function, another set of parentheses, another set of brackets, and then let's
02:57split that open on the brackets.
02:59So this function here is referred to as a callback function.
03:02So what this means is after the animation happens over 250 milliseconds,
03:07anything we put inside of here will then be executed.
03:09So we're going to want to come down here and cut this line that sets the display
03:14to 'none,' come inside of this callback function and then paste that in here.
03:20So the display setting to none will not happen until after the tooltip
03:23animates back down to 0. So to test this, let's hit Save, make sure all your
03:28files are saved. Let's come back to our operating system and let's reload this in our browser.
03:34So now inside the browser, I'll rollover a tooltip trigger, the tooltip fades in,
03:39moves with my mouse and then fades away when I move out, and this works for all
03:44of the tooltips on the screen.
03:47So that completes adding a tooltip into your website with jQuery.
03:50In the next movie, we'll talk about some CSS properties that may interfere with
03:54having elements track with the mouse and how you can correct for that.
Collapse this transcript
5. A Mouse Position "Gotcha"
Accounting for other CSS-positioned elements
00:00Now if you're putting your tooltip into an existing website, one problem that
00:03might pop up is that the tooltip_ container is not actually matching the position
00:07of the mouse cursor.
00:08This can happen if you have an HTML element that is the parent of your
00:12tooltip_container that has position properties on it.
00:16So to demonstrate what's happening here, I'm going to add the position relative
00:20attribute onto my page container.
00:22So this is going to do is now be tooltip_container will orient itself based
00:26on the page container.
00:27The problem that arises here is that the browser is calculating the X, Y
00:32position of the mouse, based on the viewport, or the overall size or body tag of the web page.
00:37So now we have a disconnect between the two.
00:39So to show you what that might look like, I'm going to open this up in a browser.
00:43Now when the browser is small and I roll over these tooltips, you might not be
00:47able to notice that the tooltip isn't aligned properly.
00:50But if I come in here and open a web browser up really wide, and now roll over
00:54these items, you'll see that the offset is actually pretty substantial.
00:58So when I roll over these tooltips, what you'll notice is the amount of distance
01:01that this is off, matches the amount of distance between the edge of the browser
01:06and the edge of my page container.
01:09So it's this value that creates that discrepancy. Again, the browser is
01:12calculating from the viewport, the tooltip is now positioning itself based on the parent.
01:18So my first recommendation is to always put the tooltip_container outside of any
01:22HTML elements that have absolute positioning.
01:25However, if it's a website that you're working on and you don't have access to
01:28put the tooltip in a place that's not affected by position, the jQuery
01:32instruction you want to look up is called offset.
01:35So what you can do is detect the offset of the parent that's doing the absolute
01:39positioning, and then calculate or adjust the position of your tooltip in very
01:44much the same way we've calculated the height and width positions.
01:47Now this may take a few times and some trial and error to get the tooltip to
01:52align properly, if you're going to be overriding the offset.
01:55But once you figure that out you're going to get the exact same user experience
01:59that we had with the template that we used in this course.
02:01So now that we've completed our tooltip, in the next movie we will take a look
02:05at where you might want to go from here.
Collapse this transcript
Where to Go from Here
Exploring resources for additional menu plug-ins
00:00Now in this course we touched on a couple different technologies.
00:02The first one is HTML5. The layout that I provided as a starting point uses
00:07HTML5 and we also added in a Google shiv, which enables HTML5 for older
00:12browsers, such as IE 7 and 8.
00:14And if you take a look at the courses that I have on lynda.com, there is a
00:17Create an HTML5 Video Gallery; this particular course walks you step-by-step
00:22through adding in the Google shiv and showing you exactly how that HTML5
00:26translates to older browsers.
00:28The other thing we did in this course was we took a look at different CSS3 properties.
00:32We looked at the RGBA color space to do Alpha, we also look that rounded
00:36corners, and so I also recommend the CSS3 First Look on the lynda.com library to
00:42get a sense of what's new in CSS3.
00:45And then finally, we added in a fade effect into our tooltip. This as part of a
00:50rollover animation, and I have the examples of additional animated rollovers
00:53that you can create using jQuery in combination with HTML and CSS.
00:58So with that, that concludes Creating a Tooltip for your web site using
01:01jQuery, and I really appreciate you watching my course.
Collapse this transcript


Suggested courses to watch next:

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


jQuery Essential Training (4h 52m)
Joe Marini


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 100,984 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,943 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