Start learning with our library of video tutorials taught by experts. Get started

Create a Tooltip with jQuery

Attaching the tip container to the mouse


From:

Create a Tooltip with jQuery

with Chris Converse

Video: Attaching the tip container to the mouse

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();.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
Create a Tooltip with jQuery
54m 23s Intermediate Aug 28, 2012

Viewers: in countries Watching now:

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
Subjects:
Developer Web Programming Languages Projects Web Development
Software:
jQuery
Author:
Chris Converse

Attaching the tip container to the mouse

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.

We're going to type a comma, another set of parentheses. We want to do a calculation inside of here. We're going to type e.pageX. Now e right here is the event that's triggering this, which up here is the mousemove, so when we run this generic function, e refers back to the mouse. So e here is the mouse movement and then pageX is a JavaScript statement that's going to tell the browser to let us know where exactly the user's cursor is. So we're going to take the x position and then we're going to subtract 20 from it.

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.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Create a Tooltip with jQuery.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

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

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

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

   
submit Lightbox submit clicked