Created
October 14, 2013 18:12
-
-
Save shaneriley/6979666 to your computer and use it in GitHub Desktop.
Tooltips using jQuery UI position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.tooltip = function() { | |
// Grabs its content from the next sibling .tooltip_content element, eg: | |
// = link_to "Remove", "#", "data-tooltip" => "" | |
// .tooltip_content | |
// %p This is my tooltip | |
// Sets a class of right or left on tooltip based on what edge of the trigger it's using | |
return this.each(function() { | |
var $el = $(this), | |
$tooltip = $el.next(".tooltip_content").clone(); | |
$tooltip.removeClass("tooltip_content").addClass("tooltip"); | |
$el.on("mouseenter", function() { | |
$tooltip.appendTo(document.body).show().position({ | |
of: $el, | |
my: "left top+6", | |
at: "left bottom", | |
collision: "flip", | |
using: function(pos, dims) { | |
$tooltip.addClass(dims.horizontal).css(pos); | |
} | |
}); | |
}); | |
$el.on("mouseleave", function() { | |
$tooltip.hide().remove().removeClass("left right"); | |
}); | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment