HoverPopup - a JQuery UI Plugin
Fiddling around with JQuery, JavaScript and assorted web development goodies in my spare time, I finally have a JQuery UI plugin I can call my own. This one mimics the behaviour you can find in the Gmail chat window. When you hover over a user, you'll get a panel that has additional details and actions for that context.
/*
* $(selector).hoverpopup({
leftMargin: [optional] - specify how much to the left of the trigger element the hover popup should appear
getTriggers: function that should return a list of elements to base the popup trigger on.
getPopupContents: function that is called to populate the popup
});
*
*/
var HoverPopup = {
options: {
leftMargin: 3,
},
_init: function() {
var _this = this;
var elem = this.element;
var triggers = this.options.getTriggers(elem);
_this.options.popup = $('#hover-popup-1');
if (_this.options.popup.length == 0) {
$('body').append("<div id='hover-popup-1' class='hover-popup'></div>");
_this.options.popup = $('#hover-popup-1');
_this.options.popup.hover(function(evt) {
clearTimeout(_this.options.t);
}, function(evt) {
});
_this.options.popup.hide();
$('html').click(_this.options.clickHandler = function(evt) {
if ($('#hover-popup-1:visible').length > 0) {
var o = _this.options.popup.offset();
var h = _this.options.popup.height();
var w = _this.options.popup.width();
if ((evt.pageX >= o.left && evt.pageX < o.left + w) &&
(evt.pageY >= o.top && evt.pageY < o.top + h)) {
// do nothing
} else {
_this.options.popup.fadeOut(_this.options.hideCallback);
}
}
});
}
for (var i = 0; i < triggers.length; i++) {
$(triggers[i]).hover(function(evt) {
_this.options.enter = setTimeout(function() {
var src = evt.currentTarget;
_this.options.popup.empty();
_this.options.popup.append(_this.options.getPopupContents(src));
clearTimeout(_this.options.t);
_this.options.popup.fadeIn(_this.options.showCallback);
_this.options.popup.offset(_this._calculateOffset(src));
}, 200);
}, function(evt) {
clearTimeout(_this.options.enter);
_this.options.t = setTimeout(function() {
_this.options.popup.fadeOut(_this.options.hideCallback);
}, 300);
});
}
},
destroy: function() {
$.Widget.prototype.destroy.apply(this, arguments);
_this.options.popup.unbind();
$('body').remove(_this.options.popup);
$('html').unbind(_this.options.clickHandler);
},
_calculateOffset: function(src) {
var w = this.options.popup.width();
var h = this.options.popup.height();
var jsrc = $(src);
var off = jsrc.offset();
var l = off.left - w - this.options.leftMargin;
if (l < 0) {
l = off.left + jsrc.width() + this.options.leftMargin;
}
var t = off.top;
var pageH = $(window).height();
if (t + h > pageH) {
t -= (t + h - pageH - 5);
}
return { "top" : t, "left" : l};
},
}
$.widget("ui.hoverpopup", HoverPopup);
The CSS code follows - which is not much really. You can style the contents of the popup which is your UI code. I suppose I could add this to the component itself and reduce the number of files.
#hover-popup-1 {
display: block;
position: absolute;
z-index: 10000;
}
Usage:
$("#div1").hoverpopup({



