Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. //variables
  2. var targets = $('[rel~=tooltip]').filter(':not(:input)'),
  3. targetsin = $('input[rel~=tooltip]'),
  4. targetText = false,
  5. targetInput = false,
  6. tooltip = false,
  7. title = false;
  8.  
  9. // bind on mouseenter/hover
  10. targets.on('mouseenter', function() {
  11. targetText = $(this);
  12. tip = targetText.attr('title');
  13. tooltip = $('<div id="tooltip"></div>');
  14. ttip(targetText);
  15. });
  16.  
  17. // bind on focusin/focus
  18. targets.on('focusin', function(){
  19. targetInput = $(this);
  20. tip = targetInput.attr('title');
  21. tooltip = $('<div id="tooltip"></div>');
  22. ttip(targetInput);
  23. });
  24.  
  25. // tooltip functions
  26. var ttip = function(target){
  27. //targets.bind('mouseenter', function(){
  28.  
  29. // if no title return false
  30. if (!tip || tip === '')
  31. return false;
  32.  
  33. //remove title attribute & create tooltip div
  34. target.removeAttr('title');
  35. tooltip.css('opacity', 0)
  36. .html(tip)
  37. .appendTo('body');
  38.  
  39. // tooltip positioning & animate
  40. var init_tooltip = function() {
  41. if ($(window).width() < tooltip.outerWidth() * 1.5)
  42. tooltip.css('max-width', $(window).width() / 2);
  43. else
  44. tooltip.css('max-width', 340);
  45.  
  46. var pos_left = target.offset().left + (target.outerWidth() / 2) - (tooltip.outerWidth() / 2),
  47. pos_top = target.offset().top - tooltip.outerHeight() - 20;
  48.  
  49. if (pos_left < 0) {
  50. pos_left = target.offset().left + target.outerWidth() / 2 - 20;
  51. tooltip.addClass('left');
  52. } else
  53. tooltip.removeClass('left');
  54.  
  55. if (pos_left + tooltip.outerWidth() > $(window).width()) {
  56. pos_left = target.offset().left - tooltip.outerWidth() + target.outerWidth() / 2 + 20;
  57. tooltip.addClass('right');
  58. } else
  59. tooltip.removeClass('right');
  60.  
  61. if (pos_top < 0) {
  62. var pos_top = target.offset().top + target.outerHeight();
  63. tooltip.addClass('top');
  64. } else
  65. tooltip.removeClass('top');
  66.  
  67. tooltip.css({
  68. left: pos_left,
  69. top: pos_top
  70. })
  71. .animate({
  72. top: '+=10',
  73. opacity: 1
  74. }, 50);
  75. };
  76.  
  77. // initiate tooltip & on resize
  78. init_tooltip();
  79. $(window).resize(init_tooltip);
  80.  
  81. // remove tooltip
  82. var remove_tooltip = function() {
  83. tooltip.animate({
  84. top: '-=10',
  85. opacity: 0
  86. }, 50, function() {
  87. $(this).remove();
  88. });
  89.  
  90. // reapply title attribute
  91. target.attr('title', tip);
  92. };
  93.  
  94.  
  95. targetText.bind('mouseleave', remove_tooltip);
  96. targetInput.bind('focusout', remove_tooltip);
  97. tooltip.bind('click', remove_tooltip);
  98. tooltip.bind('focusout', remove_tooltip);
  99. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement