Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
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.
Now we need to work on actually moving the tooltip container to match where the user's mouse is. So we're going to come inside of the mousemove area here, let's add a few Returns, and we want to start by declaring a variable. We're going to say var, space, toolTipWidth, we're going to set that equal to and we want to figure out how wide the actual tooltip container is. So dollar sign, parentheses, two tick marks for string literal, #tooltip_container, outside of the parentheses we're going to type .outerWidth();.
Next line, second variable, toolTipHeight = $(), tick marks for string literal, pound sign ('#tooltip_container').outerHeight. Now we've set a maximum-width of 200 pixels but the height can actually vary based on the HTML data that we're pulling in here or the text data that we're pulling in up here, and so depending on how much content goes in there, the actual size of that tooltip container is going to change.
So we want to declare the height and the width and variables so that we can refer back to them. So after the variables, let's start with $(), tick marks inside for a string literal. We're going to target tooltip container again. Outside of the parentheses we're going to type .css();. Inside of the parentheses we're going to start with the string literal, two tick marks, we're going to set the left property outside of the string literal but still inside of the parentheses.
That way the tooltip will be a little bit to the right of where the actual x value is, of where the cursor hits the tooltip. Now outside of the parentheses, plus sign, two tick marks for string literal, and then px. So we're taking whatever the x value is, subtracting 20 pixels and making this whole statement here a CSS property with the px on it, so that's going to be the new left property. Let's hit a Return. Let's come up here and copy this whole line, let's paste it on the next line, let's come down here and instead of left, we're going to set the 'top' value to e.pageY, and then we're going to add 20 pixels to this.
Now all of this is happening inside of the mousemove function up here, so let's save our file, let's come back out to the browser. Let's hit Reload, and now when we move over tooltip, we'll see that the position of that tooltip now sticks near the cursor and as we move the mouse, we constantly update the position of that object as well. Same thing for the HTML-based tooltip on the right-hand side. Now one thing you'll notice when I rollover the tooltip on the right-hand side, the tooltip, since it's aligning itself to the left and positioning to the right, actually moves off the browser page.
So in the next movie we're going to work on detecting where the tooltips are and if they are on the right-hand side of the screen, we're going to position the tooltip toward the left. So the tooltips will always position themselves, so that they're visible to the user.
There are currently no FAQs about Create a Tooltip with jQuery.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.