easyTooltip.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Easy Tooltip 1.0 - jQuery plugin
  3. * written by Alen Grakalic
  4. * http://cssglobe.com/post/4380/easy-tooltip--jquery-plugin
  5. *
  6. * Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
  7. * Dual licensed under the MIT (MIT-LICENSE.txt)
  8. * and GPL (GPL-LICENSE.txt) licenses.
  9. *
  10. * Built for jQuery library
  11. * http://jquery.com
  12. *
  13. */
  14. (function($) {
  15. $.fn.easyTooltip = function(options){
  16. // default configuration properties
  17. var defaults = {
  18. xOffset: 10,
  19. yOffset: 25,
  20. tooltipId: "easyTooltip",
  21. clickRemove: false,
  22. content: "",
  23. useElement: ""
  24. };
  25. var options = $.extend(defaults, options);
  26. var content;
  27. this.each(function() {
  28. var title = $(this).attr("title");
  29. $(this).hover(function(e){
  30. content = (options.content != "") ? options.content : title;
  31. content = (options.useElement != "") ? $("#" + options.useElement).html() : content;
  32. $(this).attr("title","");
  33. if (content != "" && content != undefined){
  34. $("body").append("<div id='"+ options.tooltipId +"'>"+ content +"</div>");
  35. $("#" + options.tooltipId)
  36. .css("position","absolute")
  37. .css("top",(e.pageY - options.yOffset) + "px")
  38. .css("left",(e.pageX + options.xOffset) + "px")
  39. .css("display","none")
  40. .fadeIn("fast")
  41. }
  42. },
  43. function(){
  44. $("#" + options.tooltipId).remove();
  45. $(this).attr("title",title);
  46. });
  47. $(this).mousemove(function(e){
  48. $("#" + options.tooltipId)
  49. .css("top",(e.pageY - options.yOffset) + "px")
  50. .css("left",(e.pageX + options.xOffset) + "px")
  51. });
  52. if(options.clickRemove){
  53. $(this).mousedown(function(e){
  54. $("#" + options.tooltipId).remove();
  55. $(this).attr("title",title);
  56. });
  57. }
  58. });
  59. };
  60. })(jQuery);