Skip to content

Instantly share code, notes, and snippets.

@tarex
Created October 14, 2012 03:29
Show Gist options
  • Save tarex/3887142 to your computer and use it in GitHub Desktop.
Save tarex/3887142 to your computer and use it in GitHub Desktop.
jquery plugin - tooltip
// tooltip , i used in http://fbviralpictures.com/
(function(jQuery) {
jQuery.fn.k_tooltip = function(options) {
var defaults = {
use: 'title',
tooltipClass: 'tooltip'
};
var options = jQuery.extend(defaults, options),
container = jQuery('<div></div>').addClass('tooltip_container').appendTo('body');
return this.each(function() {
var element = jQuery(this),
clickLink = element.parent().find('.livemore').attr('href'),
tooltip;
if (options.use == 'title') {
tooltip = jQuery('<div></div>').addClass(options.tooltipClass).text(element.attr('title'));
} else if (options.use == 'href') {
tooltip = jQuery('<div></div>').addClass(options.tooltipClass);
var img_src = element.attr('href');
tooltip.append("<img src='" + img_src + "' alt='' />");
} else if (options.use == 'src') {
tooltip = jQuery('<div></div>').addClass(options.tooltipClass);
var img_src = element.attr('src');
tooltip.append("<img src='" + img_src + "' alt='' />");
} else {
if (options.use != '') {
tooltip = jQuery(this).find(options.use).clone();
} else {
tooltip = jQuery(this).clone();
}
}
tooltip.appendTo(container);
tooltip.find('img').css({
height: '312px',
width: '527px'
});
element.attr('href', clickLink);
element.mousemove(function(pos) {
var border_top = jQuery(window).scrollTop(),
border_right = jQuery(window).width(),
left_pos, top_pos, offset = 30;
if (border_right - (offset * 2) >= tooltip.width() + pos.pageX) {
left_pos = pos.pageX + offset;
} else {
left_pos = pos.pageX - tooltip.width() - offset;
}
if (border_top + (offset * 2) >= pos.pageY - tooltip.height()) {
top_pos = border_top + offset;
} else {
top_pos = pos.pageY - tooltip.height() - offset;
}
tooltip.css({
display: 'block',
left: left_pos,
top: top_pos
});
}).mouseout(function(pos) {
tooltip.css({
display: 'none'
});
})
});
}
})(jQuery);
// here is the usage
$(function(){
$('.class_or_id').k_tooltip({use:'src'});
});
// and some css would be nice
/*
.tooltip {
left: -9999px;
padding: 8px;
position: absolute;
top: 0;
z-index: 200;
padding: 5px;
background: #eee;
border: 1px solid #aaa;
}
.tooltip img {
display: block;
border: none;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment