New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

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

Create a Tooltip with jQuery
Illustration by

Positioning the tooltip above or below the cursor


From:

Create a Tooltip with jQuery

with Chris Converse

Video: Positioning the tooltip above or below the cursor

So now we're going to add in detection for the pageHeight. This is going to allow us to do two things; one, we can put the tooltip_ container above the cursor, and two, we can make sure that the tooltip doesn't touch the top of the browser. Now in order to get a link into the top of our page let's come back to our index.html file. In the heading area, we have an anchor link with the class of logo, let's come in here and add our tooltip properties to this link. First, let's add a second class onto here, so after logo inside of the value for class, let's hit a space, let's add in tooltip.

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

Positioning the tooltip above or below the cursor

So now we're going to add in detection for the pageHeight. This is going to allow us to do two things; one, we can put the tooltip_ container above the cursor, and two, we can make sure that the tooltip doesn't touch the top of the browser. Now in order to get a link into the top of our page let's come back to our index.html file. In the heading area, we have an anchor link with the class of logo, let's come in here and add our tooltip properties to this link. First, let's add a second class onto here, so after logo inside of the value for class, let's hit a space, let's add in tooltip.

Now after the attribute for href, let's add in data-tip-type, equals, two quotes, put in "text". Let's add one more attribute, data-tip- source, equals, two quotes, and I'll just type in here, "This is the logo". Let's hit Save, now the logo will act as a tooltip as well, let's come to back to our tooltip.js file.

Now inside of here, inside of mousemove, after we do the pageWidth detection, let's hit a few Returns and what we're going to do in here is but a standard JavaScript conditional statement. So if () and then beginning and ending bracket, split that open on the brackets. Inside of the if statement what we want to do is test for the pageY, so e.pageY, and if that is greater than 100, and we're going to test to see whether this is greater than or lower than 100 pixels, because in the CSS we've set the header area to take 100 pixels, so we'll know if the cursor is below the 100 pixel mark.

And if it is, what we're going to do inside up here is target the tooltip_container, so $() tick marks for string literal, a pound sign ('#tooltip_container'), outside of the parentheses we're going to type .css () then a semicolon, inside of the parentheses another string literal. We're going to type 'top,' outside of the string literal hit comma, another set of parentheses, outside of the parentheses plus another string literal 'px'.

Now inside of the parentheses, e.pageY-, then another set of parentheses, now inside of the innermost parentheses, let's come up here and let's copy toolTipHeight, copy that variable name, let's come down here, let's paste that and then put +20. So what we're doing here is taking the height of the tooltip, adding 20 pixels and then subtracting that from the position of the cursor. Now what this is going to do is position the tooltip above where the cursor intersects with any HTML element with the tooltip class.

Now let's add an else onto this condition, so after the bracket type else, another set of brackets. Let's come down here and let's take the css 'top' property we were using before. Let's cut that to the clipboard, let's paste it into here. Now to test this let's choose Save, let's come back up to the browser and hit Reload. Now you'll see our tooltips are now going above the cursor. Now while I come up here and rollover the logo, notice that the tooltip is now activated for the logo, but the position of the tooltip is now showing up underneath the position of the cursor.

So now that we're properly detecting for height and width position, in the next movie we'll hide the tooltip, so that it only shows when somebody rolls over a tooltip item.

There are currently no FAQs about Create a Tooltip with jQuery.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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 Upgrade 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
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.

Are you sure you want to delete this note?

No

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.