Skip to content

Instantly share code, notes, and snippets.

@stormwarning
Created May 21, 2017 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stormwarning/b6f4ad86f3679f901d86561ff722235d to your computer and use it in GitHub Desktop.
Save stormwarning/b6f4ad86f3679f901d86561ff722235d to your computer and use it in GitHub Desktop.
Tooltip script from culturedcode.com
tooltip = {
elements: void 0,
CLASS_BUBBLE: "tooltip-bubble",
CLASS_CONTENT: "tooltip-content",
CLASS_HEADER: "tooltip-header",
CLASS_FOOTER: "tooltip-footer",
STATE_INIT: "is-initialized",
STATE_VISIBLE: "is-visible",
init: function(e) {
this.elements = e;
for (var t = 0; t < this.elements.length; ++t) {
var i = $(this.elements[t])
, a = this.createBubbleElement(i);
i.data("bubble-element", a),
$(".page").eq(0).append(a)
}
browser.isTouch ? ($("." + this.CLASS_BUBBLE).on("touchstart", $.proxy(this.handleBubbleTouchStart, this)),
this.elements.on("touchstart", $.proxy(this.handleTouchStart, this)),
this.elements.on("click", $.proxy(this.handleTouchClick, this))) : (this.elements.on("mouseenter", $.proxy(this.handleMouseEnter, this)),
this.elements.on("mouseleave", $.proxy(this.handleMouseLeave, this))),
this.elements.addClass(this.STATE_INIT)
},
createBubbleElement: function(e) {
var t = e.attr("title")
, i = e.data("content")
, a = e.data("header")
, s = e.data("footer");
t && (e.data("content", t),
e.removeAttr("title"),
i = t);
var r = "";
e.hasClass("style-large") && (r += "style-large ");
var n = $('<div class="' + this.CLASS_BUBBLE + " " + r + '"></div>');
if (i) {
var o = $('<div class="' + this.CLASS_CONTENT + '">' + i + "</div>");
n.append(o)
}
if (a) {
var l = $('<div class="' + this.CLASS_HEADER + '">' + a + "</div>");
n.prepend(l)
}
if (s) {
var d = $('<div class="' + this.CLASS_FOOTER + '">' + s + "</div>");
n.append(d)
}
return n
},
showBubble: function(e) {
var t = e.data("bubble-element")
, i = 5
, a = 0
, s = 0
, r = 0
, n = t.outerWidth()
, o = t.outerHeight()
, l = e.offset().left
, d = e.offset().top
, h = e.outerWidth()
, T = e.outerHeight();
s = l + h / 2 - n / 2,
r = d - 0 - o;
var u = $(document).width();
s = Math.min(s, u - 5 - n),
s = Math.max(s, 5),
s = Math.round(s),
r = Math.round(r),
t.addClass(this.STATE_VISIBLE),
t.css("left", s),
t.css("top", r)
},
hideAllBubbles: function() {
$("." + this.CLASS_BUBBLE).removeClass(this.STATE_VISIBLE)
},
handleTouchStart: function(e) {
e.preventDefault(),
e.stopPropagation();
var t = $(e.currentTarget);
t.data("bubble-element").hasClass(this.STATE_VISIBLE) && (t.attr("href") && (window.location = t.attr("href")));
this.hideAllBubbles(),
this.showBubble(t),
$(document).one("touchstart", $.proxy(this.handleTouchDismiss, this))
},
handleTouchDismiss: function(e) {
e.stopPropagation(),
this.hideAllBubbles()
},
handleTouchClick: function(e) {
e.preventDefault(),
e.stopPropagation();
var t = $(e.currentTarget);
!1 === t.hasClass(this.STATE_VISIBLE) && (this.hideAllBubbles(),
this.showBubble(t),
$(document).one("touchstart", $.proxy(this.handleTouchDismiss, this)))
},
handleBubbleTouchStart: function(e) {
e.preventDefault(),
e.stopPropagation(),
this.hideAllBubbles()
},
handleMouseEnter: function(e) {
e.preventDefault(),
e.stopPropagation(),
this.hideAllBubbles(),
this.showBubble($(e.currentTarget))
},
handleMouseLeave: function(e) {
this.hideAllBubbles(),
e.preventDefault()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment