Guest User

Untitled

a guest
Sep 25th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. # Easy tooltips for elements.
  2. #
  3. # Examples
  4. # <span data-tooltip="The fuck was that?">The fuck is this?</span>
  5. #
  6. # # Init tooltip
  7. # $ ->
  8. # $('[data-tooltip]').tooltip()
  9. class Tooltip
  10. @el: null
  11. @tooltip: null
  12. @open: false
  13. @closeTimer: null
  14. @topMargin: null
  15. constructor: (el)->
  16. @el = $(el)
  17. @createTooltip()
  18. @bindEvents()
  19.  
  20. bindEvents: =>
  21. @el.bind('mouseover', @mouseOver)
  22. @el.bind('mouseout', @mouseOut)
  23.  
  24. mouseOver: =>
  25. if @open
  26. clearTimeout(@closeTimer)
  27. else
  28. @show()
  29.  
  30. tooltipMouseOver: =>
  31. clearTimeout(@closeTimer)
  32.  
  33. mouseOut: =>
  34. @closeTimer = setTimeout(@hide, 100)
  35.  
  36. createTooltip: =>
  37. el = $('<div></div>')
  38. .attr('class', 'tooltip')
  39. .append($('<i>'))
  40. .append($("<span class='inner'>#{@el.data('tooltip')}</div>"))
  41. .bind('mouseover', @tooltipMouseOver)
  42. .bind('mouseout', @mouseOut)
  43. @tooltip = el
  44.  
  45. show: =>
  46. return if @open
  47. @open = true
  48. $('body').append(@tooltip)
  49. @topMargin = @tooltip.css('margin-top')
  50. pos = @findPos()
  51. @tooltip.css('top', "#{pos.top}px")
  52. .css('left', "#{pos.left}px")
  53. @tooltip.animate({opacity: 1, 'margin-top': '0px'}, 200)
  54.  
  55. hide: =>
  56. @tooltip.animate { opacity: 0, 'margin-top': @topMargin }, 200, =>
  57. @tooltip.detach()
  58. @open = false
  59.  
  60. findPos: =>
  61. # Find position of middle of element
  62. pos = @el.offset()
  63. pos.left += @el.width() / 2
  64.  
  65. # Adjust position based on size of tooltip
  66. pos.top -= @tooltip.outerHeight()
  67. pos.left -= @tooltip.outerWidth() / 2
  68.  
  69. pos
  70.  
  71.  
  72. # Create jQuery plugin
  73. jQuery.fn.extend
  74. tooltip: ->
  75. @.each ()->
  76. new Tooltip(@)
Add Comment
Please, Sign In to add comment