Created
September 24, 2012 20:32
-
-
Save msarchet/3778195 to your computer and use it in GitHub Desktop.
A Javascript Function to position one element around a different element
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 positionIt($element, $anchorElement, verticalPercent, horizontalPercent) | |
{ | |
if(!($localElement instanceof jQuery)) | |
{ | |
throw '$element must be a jQuery object'; | |
return; | |
} | |
if(!($anchorElement instanceof jQuery)) | |
{ | |
throw '$anchorElement must be a jQuery object'; | |
return; | |
} | |
if(verticalPercent > 100 || verticalPercent < 0) | |
{ | |
throw 'verticalPercent must be between 0 and 100'; | |
} | |
if(horizontalPercent > 100 || horizontalPercent < 0) | |
{ | |
throw 'horizontalPercent must be between 0 and 100'; | |
} | |
//Get the starting offset for the element; | |
var baseOffset = $anchorElement.offset(); | |
var width = $anchorElement.outerWidth(); | |
var height = $anchorElement.outerHeight(); | |
var top = baseOffset.top + (height * parseFloat(verticalPercent) / 100.0); | |
var left = baseOffset.left + (width * parseFloat(horizontalPercent) / 100.0); | |
$element.css({'top': top, 'left': left, 'position': 'absolute'}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to out inside vs outside positions.