-
Notifications
You must be signed in to change notification settings - Fork 1
/
tooltip.coffee
42 lines (36 loc) · 1.15 KB
/
tooltip.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$ = jQuery
$.fn.extend
NRTooltip: (options) ->
settings =
delay: false
elements: false
settings = $.extend settings, options
return @each () ->
new NRTooltip this, settings
class NRTooltip
constructor: (@element, @settings) ->
if $('.NRTooltip').length == 0
$('body').append('<div class="NRTooltip"><span></span><div class="NRTooltip_tip"></div></div>')
if @settings.elements
@elements = @settings.elements
$(@element).delegate(@elements, "mouseover", (e) ->
showTip(e)
)
$(@element).delegate(@elements, "mouseout", (e) ->
hideTip(e)
)
else
$(@element).mouseover (e) ->
showTip(e)
$(@element).mouseout (e) ->
hideTip(e)
showTip = (e) ->
elem = $(e.target)
position = elem.offset()
$('.NRTooltip span').html($(elem).data('tooltip'))
$('.NRTooltip').addClass('visible')
position.top -= $('.NRTooltip').outerHeight() + 6
position.left += elem.outerWidth()/2 - $('.NRTooltip').outerWidth()/2
$('.NRTooltip').offset(position)
hideTip = (e) ->
$('.NRTooltip').removeClass('visible')