We launched a new IT training category! Check out the 140+ courses now.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Attaching the tip container to the mouse

From: Create a Tooltip with jQuery

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

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.

Show transcript

This video is part of

Image for Create a Tooltip with jQuery
Create a Tooltip with jQuery

18 video lessons · 5250 viewers

Chris Converse
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.


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.

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 preferences from 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

Your file was successfully uploaded.

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.