Move Tooltip With Mouse or Cursor by Using jQuery in Web API

Introduction

This article describes how to move a Tooltip by moving the cursor using jQuery. When you move the mouse or curser, the tooltip also moves depending on the cursor or mouse.

The following describes the procedure to do that.

Step 1

First create an application:

  • Start Visual Studio 2012.
  • From the start window select "New Project".
  • From the new project window select "Installed" -> "Visual C#" -> "Web".
  • Select "ASP.NET MVC4 Web Application" and click on the "OK" button.

    tool.jpg

  • From the "MVC4 Project" window select "Web API".

    tool1.jpg

  • Click on the "OK" button.

Step 2

Add a model class in the model folder.

  • In the "Solution Explorer".
  • Right-click on the Model Folder and select "Add" -> "Class".
  • Select "Installed" -> "Visual C# " and select "Class".

    tool2.jpg

  • Click the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace MoveToolTipAPI.Models  
  6. {  
  7.     public class ModelClass  
  8.     {  
  9.         public List<Employee> EmployeeList { getset; }  
  10.     }  
  11.     public class Employee  
  12.     {  
  13.         public int ID { getset; }  
  14.         public string Name { getset; }  
  15.         public int EmpChart { getset; }  
  16.     }  
  17. } 

Step 3

In the "HomeController" add some code. This file exists:

  • In the "Solution Explorer".
  • Expand the "Controller" folder.
  • Select the "HomeController".
    tool3.jpg

Add the following code.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MoveToolTipAPI.Models;  
  7. namespace MoveToolTipAPI.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         public ActionResult Index()  
  12.         {  
  13.             ModelClass mdlc = new ModelClass();  
  14.             List<Employee> objemp = new List<Employee>();  
  15.             objemp = GetEmployeeList();  
  16.             mdlc.EmployeeList = objemp;  
  17.             return View(mdlc);  
  18.         }  
  19.         public List<Employee> GetEmployeeList()  
  20.         {  
  21.             List<Employee> mdlc = new List<Employee>();  
  22.             mdlc.Add(new Employee { ID = 1, Name = "Employee1",EmpChart=19 });  
  23.             mdlc.Add(new Employee { ID = 2, Name = "Employee2" ,EmpChart=34});  
  24.             mdlc.Add(new Employee { ID = 3, Name = "Employee3" ,EmpChart=12});  
  25.             mdlc.Add(new Employee { ID = 4, Name = "Employee4" ,EmpChart=67});  
  26.             mdlc.Add(new Employee { ID = 5, Name = "Employee5" ,EmpChart=45});  
  27.             mdlc.Add(new Employee { ID = 6, Name = "Employee6",EmpChart=10 });  
  28.             mdlc.Add(new Employee { ID = 7, Name = "Employee7" ,EmpChart=20});  
  29.             return mdlc;  
  30.         }  
  31.     }  
  32. }  

 

Step 4

In  the "View" use the following procedure:

  • In the "Solution Explorer".
  • Expand the "Views" folder.
  • Select "Home" -> "Index.cshtml".
    tool4.jpg

Add the following code:

 

  1. @model MoveToolTipAPI.Models.ModelClass  
  2. @{  
  3.     ViewBag.Title = "Grid";  
  4. }  
  5. <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/thems/base/jquery-ui.css" />  
  6.  <script src="http://code.jquery.com/jquery-1.8.2.js"></script>  
  7. <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>  
  8. <style type="text/css">  
  9. .ui-tooltip { font-size:12pt;  
  10. font-family:Arial;  
  11. } body { font-size:12pt;  
  12. padding:10px;  
  13. font-family:Arial;  
  14. } </style>  
  15. <script type="text/javascript">  
  16. $(function () {  
  17. $('.demotooltip').tooltip({  
  18. track: true  
  19. });  
  20. }); </script>  
  21. <h2>@ViewBag.Message</h2>  
  22. @{  
  23. var grid = new WebGrid(source: Model.EmployeeList,rowsPerPage: 10);  
  24. }  
  25. @grid.GetHtml(alternatingRowStyle:"even",  
  26. columns:grid.Columns(  
  27. grid.Column("ID","ID"),  
  28. grid.Column("Name",header:"Name"),  
  29. grid.Column("EmpChart",header: "WorkingAverage" ,format:@<text><img src="https://chart.googleapis.com/chart?cht=bhs&chd=t:@item.EmpChart&chs=200x30" alt="@item.EmpChart" title="WorkingAverage of @item.Name is @item.EmpChart"   
  30.     class="demotooltip" /></text>)  
  31. )  
  32. )  

 

Step 5

Execute the application. The output will be as:

tool5.jpg

Move the cursor to any graph. It then displays a tooltip with a message.

 tool6.jpg


Similar Articles