Saturday, September 21, 2019

jQuery UI Tooltip

The native tooltip is replaced by jQuery UI tooltip widget because the jQuery UI tooltip allows customization and adds new themes.

What is Tooltip?

Tooltips are used with the element to display a title in the title box next to the element, when you hover the element with your mouse. Tooltips can be attached to any element. If you want to display tooltip, just add title attribute to input elements and the title attribute value will be used as tooltip.

What does jQueryUI Tooltip do?
The jQuery UI tooltip() method adds tooltip to any element on which you want to display tooltip. It gives a fade animation by default to show and hide the tooltip, compared to just toggling the visibility.
Syntax:
You can use the tooltip() method in two forms.
  1. $(selector, context).tooltip (options) Method  
  1. (selector, context).tooltip ("action", [params]) Method  

First Method

The tooltip (options) method specifies that a tooltip can be added to an HTML element. The options parameter is an object that specifies the behavior and appearance of the tooltip.
Syntax:
  1. $(selector, context).tooltip(options);  
You can use one or more options at a time using Javascript object. In the case of more than one options, you will separate them using a comma as follows:
  1. $(selector, context).tooltip({option1: value1, option2: value2..... });  
Following is a list of different options that can be used with this method.
OptionDescription
contentThis option is used to represent the content of a tooltip. By default its value is function returning the title attribute.
disabledIf you set this option to true, it disables the tooltip. By default its value is false.
hideThis option is used to represent the animation effect when hiding the tooltip. By default its value is true.
itemsThis option specifies which items can show tooltips. By default its value is [title].
positionThis option is used to indicate the position of the tooltip with respect to the associated target element. By default its value is function returning the title attribute. Its possible values are: my, at, of, collision, using, within.
showThis option represents how to animate the showing of tooltip. By default its value is true.
tooltipClassThis option is a class which can be added to the tooltip widget for tooltips such as warning or errors. By default its value is null.
trackIf you set this option to true, the tooltip follows/tracks the mouse. By default its value is false.

jQuery UI Tooltip() example 1

Let's take a simple example to demonstrate the tooltip functionality passing no parameters to the tooltip() method.
  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <title>jQuery UI Tooltip functionality</title>  
  6. <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7. <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.   
  10. <!-- Javascript -->  
  11. <script>  
  12. $(function() {  
  13. $("#tooltip-1").tooltip();  
  14. $("#tooltip-2").tooltip();  
  15. });  
  16. </script>  
  17. </head>  
  18. <body>  
  19. <!-- HTML -->  
  20. <label for="comment">What is your comment:</label>  
  21. <input id="tooltip-1" title="We are asking your comment just for feedback">  
  22. <p><a id="tooltip-2" href="#" title="JavaTpoint is the best tutorial website.">  
  23. I also have a tooltip  
  24. </a></p>  
  25. </body>  
  26. </html>  

jQueryUI Tooltip() method example 2

Use of position:
Let's take an example to demonstrate the usage of option position in the tooltip function of jQuery UI.
  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <title>jQuery UI Tooltip functionality</title>  
  6. <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7. <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9. <!-- CSS -->  
  10. <style>  
  11. body {  
  12. margin-top: 100px;  
  13. }  
  14.   
  15. .ui-tooltip-content::after, .ui-tooltip-content::before {  
  16. content: "";  
  17. position: absolute;  
  18. border-style: solid;  
  19. display: block;  
  20. left: 90px;  
  21. }  
  22. .ui-tooltip-content::before {  
  23. bottom: -10px;  
  24. border-color: #AAA transparent;  
  25. border-width: 10px 10px 0;  
  26. }  
  27. .ui-tooltip-content::after {  
  28. bottom: -7px;  
  29. border-color: white transparent;  
  30. border-width: 10px 10px 0;  
  31. }  
  32. </style>  
  33. <!-- Javascript -->  
  34. <script>  
  35. $(function() {  
  36. $( "#tooltip-7" ).tooltip({  
  37. position: {  
  38. my: "center bottom",  
  39. at: "center top-10",  
  40. collision: "none"  
  41. }  
  42. });  
  43. });  
  44. </script>  
  45. </head>  
  46. <body>  
  47. <!-- HTML -->  
  48. <label for="name">Name:</label>  
  49. <input id="tooltip-7" title="Please use capital letters.">  
  50. </body>  
  51. </html>  

jQuery UI Tooltip() method example 3

Use of content, track and disabled:
The following example demonstrates the usage of options content, track and disabled.
  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <title>jQuery UI Tooltip functionality</title>  
  6. <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7. <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.   
  10. <!-- Javascript -->  
  11. <script>  
  12. $(function() {  
  13. $( "#tooltip-3" ).tooltip({  
  14. content: "<strong>Hello JavaTpoint</strong>",  
  15. track:true  
  16. }),  
  17. $( "#tooltip-4" ).tooltip({  
  18. disabled: true  
  19. });  
  20. });  
  21. </script>  
  22. </head>  
  23. <body>  
  24. <!-- HTML -->  
  25. <label for="name">Here is the message:</label>  
  26. <input id="tooltip-3" title="tooltip"><br><br><br>  
  27. <label for="name">Tooltip is disabled:</label>  
  28. <input id="tooltip-4" title="tooltip">  
  29. </body>  
  30. </html>  

Second method

The tooltip (action, params) method is used to perform an action on the tooltip elements, such as disabling the tooltip. The action is specified as a string in the first argument and optionally, you can use one or more params according to the given action.
Syntax:
  1. $(selector, context).tooltip ("action", [params]);  
Following is a list of different actions used with this method.
ActionDescription
close()This action is used to close the tooltip. This method does not accept any arguments.
destroy()This action is used to remove the tooltip functionality completely. This will return the element back to its pre-init state. This method does not accept any arguments.
disable()This action is used to deactivate the tooltip. This method does not accept any arguments.
enable()This action activates the tooltip. This method does not accept any arguments.
open()This action programmatically opens the tooltip. This method does not accept any arguments.
option(optionName)This action gets the value associated with optionName for the tooltip. This method does not accept any arguments.
option()This action gets an object containing key/value pairs representing the current tooltip options hash. This method does not accept any arguments.
option(optionName, Value)This action sets the value of the tooltip option associated with the specified optionName.
option(Options)This action sets one or more options for tooltip.
widget()This action returns a jQuery object containing the original element. This method does not accept any arguments.
Event methods used with tooltip elements:
jQueryUI triggers event methods for a particular event. Following is a list of these event methods:
Event MethodDescription
create(event,ui)It is triggered when the tooltip is created. Here event is of type event, and ui is of type object.
close(event,ui)It is triggered when the tooltip is closed. It is usually triggers on focusout or mouseleave. Here event is of type event, and ui is of type object.
open(event,ui)It is triggered when the tooltip is displayed or shown. It is usually triggered on focusin or mouseover. Here event is of type event, and ui is of type object.

jQueryUI Tooltip() method example 4

Let's take an example to demonstrate event method usage during tooltip functionality. The following example uses the open and close events.

  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Tooltip functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <!-- Javascript -->  
  10.       <script>  
  11.          $(function() {  
  12.             $('.tooltip-9').tooltip({  
  13.                items: 'a.tooltip-9',  
  14.                content: 'Are you looking for a training program?',  
  15.                show: "slideDown", // show immediately  
  16.                open: function(event, ui)  
  17.                {  
  18.                   ui.tooltip.hover(  
  19.                   function () {  
  20.                      $(this).fadeTo("slow", 0.5);  
  21.                   });  
  22.                }  
  23.             });  
  24.          });  
  25.          $(function() {  
  26.             $('.tooltip-10').tooltip({  
  27.                items: 'a.tooltip-10',  
  28.                content: 'Welcome to JavaTpoint',  
  29.                show: "fold",   
  30.                close: function(event, ui)  
  31.                {  
  32.                   ui.tooltip.hover(function()  
  33.                   {  
  34.                      $(this).stop(true).fadeTo(500, 1);   
  35.                   },  
  36.                   function()  
  37.                   {  
  38.                      $(this).fadeOut('500', function()  
  39.                      {  
  40.                         $(this).remove();  
  41.                      });  
  42.                   });  
  43.                }  
  44.             });  
  45.          });  
  46.       </script>  
  47.    </head>  
  48.    <body style="padding:100px;">  
  49.       <!-- HTML -->   
  50.       <div id="target">  
  51.          <a href="#" class="tooltip-9">Hover over me!</a><br/>  
  52.          <a href="#" class="tooltip-10">Hover over me too!</a>  
  53.       </div>  
  54.    </body>  
  55. </html>  

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...