Skip to content

Instantly share code, notes, and snippets.

@dawnerd
Created November 11, 2011 02:47
Show Gist options
  • Save dawnerd/1357041 to your computer and use it in GitHub Desktop.
Save dawnerd/1357041 to your computer and use it in GitHub Desktop.
Just The Tip (The tooltip that is)
.TooltipContainer { position: absolute; z-index: 100; font-size: 10px; display: none; background: #333; color: #fff; -moz-border-radius: 3px; -webkit-border-radius: 3px; -o-border-radius: 3px; -ms-border-radius: 3px; border-radius: 3px; box-shadow: 0 2px 5px 0 rgba(0,0,0, .4); }
.TooltipContainer .arrow { position: absolute; bottom: -4px; left: 15px; z-index: 1950; background: #333; height: 10px; width: 10px; -webkit-transform: rotate(45deg); -moz-transform: rotate(45deg); }
.TooltipContainer .content { padding: 5px; position: relative; z-index: 2000; }
/**
* tooltip.class.js
* Requires: jQuery, class.js
*
* Just the tip.
*
* Usage:
* var tooltip = new TooltipClass( [options] );
*
* Options:
* selector (string): Overrides default tooltip selector
*
* Data Values:
* data-tooltip: Content to be displayed in tooltip
*/
dmjs.TooltipClass = Class.extend({
defaults: {
selector: 'a[data-tooltip]'
},
init: function(options) {
var self = this;
this.options = $.extend({}, this.defaults, options);
this.windowWidth = $("html").width(),
this.bodyWidth = $("body").width(),
$(this.options.selector).live('mouseenter.tooltip', function(e) {
var element = $(this);
self.show(e, element);
});
$(this.options.selector).live('mouseleave.tooltip', function(e) {
var element = $(this);
self.hide(element);
});
},
show: function(e, element) {
var self = this;
this.tooltipElement = $('<div><div class="arrow"></div></div>')
.addClass('TooltipContainer')
.appendTo('body')
.show();
this.tooltipElement = $('.TooltipContainer');
this.tooltipContent = $('<div/>')
.addClass('content')
.html(element.data('tooltip'))
.appendTo(this.tooltipElement);
$('body').bind('mousemove.tooltip', function(e) {
self.updatePosition(e);
});
},
hide: function(element) {
$('body').unbind('mousemove.tooltip');
$('.TooltipContainer').remove();
},
updatePosition: function(e) {
this.tooltipElement.css({
left: e.pageX-(this.windowWidth-this.bodyWidth)/2-18,
top: e.pageY-45
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment