Created
October 16, 2012 01:22
-
-
Save msarchet/3896754 to your computer and use it in GitHub Desktop.
Quick Tooltip positioning start
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 quicktip(options) { | |
var verticalPercent = '0'; | |
var horizontalPercent = '0'; | |
var stick = false; | |
if(options.verticalPercent !== undefined) | |
{ | |
verticalPercent = options.verticalPercent; | |
} | |
if(options.horizontalPercent !== undefined) | |
{ | |
horizontalPercent = options.horizontalPercent; | |
} | |
if(options.stick !== undefined) | |
{ | |
stick = options.stick; | |
} | |
if(options.anchor === undefined) | |
{ | |
throw 'anchor must be defined'; | |
} | |
var anchor; | |
if(!(options.anchor instanceof jQuery)) | |
{ | |
options.anchor = $(options.anchor); | |
} | |
anchor = options.anchor; | |
if(options.content === undefined) | |
{ | |
throw 'content must be defined'; | |
} | |
//set content | |
var content; | |
if(!(options.content instanceof jQuery)) | |
{ | |
options.content = $(options.content); | |
} | |
content = options.content; | |
//Get the starting offset for the element; | |
var baseOffset = anchor.offset(); | |
var width = anchor.outerWidth(); | |
var height = anchor.outerHeight(); | |
var top = baseOffset.top + (height * parseFloat(verticalPercent) / 100.0); | |
if(top < 0) | |
{ | |
top = -top; | |
} | |
var left = baseOffset.left + (width * parseFloat(horizontalPercent) / 100.0); | |
if(left < 0) | |
{ | |
left = -left; | |
} | |
var isMouseStillInAnchor = function(event) { | |
var leftEdge = event.pageX >= baseOffset.left; | |
var rightEdge = event.pageX <= (baseOffset.left + width); | |
var topEdge = event.pageY >= baseOffset.top; | |
var bottomEdge = event.pageY <= (baseOffset.top + height); | |
return (leftEdge && rightEdge && topEdge && bottomEdge); | |
}; | |
content.hide(); | |
content.css({'top': top, 'left': left, 'position': 'absolute'}); | |
anchor.mouseover(function() { content.show(); }); | |
if(stick){ | |
content.click(function(event) { | |
if(!isMouseStillInAnchor(event)){ | |
content.hide(); | |
} | |
}); | |
} | |
else{ | |
anchor.mouseout(function(event) { | |
if(!isMouseStillInAnchor(event)) { | |
content.hide(); | |
} | |
}); | |
content.mouseout(function(event) { | |
if(!isMouseStillInAnchor(event)) { | |
content.hide(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment