Skip to content

Instantly share code, notes, and snippets.

@AbraaoAlves
Created September 26, 2011 00:40
Show Gist options
  • Save AbraaoAlves/1241372 to your computer and use it in GitHub Desktop.
Save AbraaoAlves/1241372 to your computer and use it in GitHub Desktop.
Create tip message through title attribute, custom data atribute or none of these(cache message).
//demo: http://jsfiddle.net/AbraaoAlves/W7j4z/18/
//use this:
//$("input:first").tip();
//$("[data-tip]").tip({tipMsgAttr: 'data-tip'});
//$("p").tip('custom message');
(function($){
var $defaults = {
tipMsgAttr : 'title',
tip : $("<span class='tip'>").css({
background : '#000', color: '#fff', padding: '5px',
display: 'none', position: 'absolute'
}).append($("<div>").css({
'border-color': 'transparent transparent #000 transparent ',
'border-style': 'solid', 'border-width': '12px 8px', height:'0px', width: '0px', position: 'absolute', bottom:'26px'
}))
};
$.fn.tip = function(options) {
options = (typeof options === 'string')? {tipMsg: options} : options;
var params = $.extend($defaults, options );
return this.each(function() {
var msg = (params.tipMsg) ?
$(this).data("tip", params.tipMsg).data("tip") :
$(this).attr(params.tipMsgAttr);
var tip = params.tip.clone();
tip.prepend(msg).appendTo('body');
tip.css({
top :$(this).offset().top + 30,
left:$(this).offset().left + 5 });
$(this).hover(
function() { tip.show();},
function() { tip.hide();});
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment