Skip to content

Instantly share code, notes, and snippets.

@msarchet
Created September 24, 2012 20:32
Show Gist options
  • Select an option

  • Save msarchet/3778195 to your computer and use it in GitHub Desktop.

Select an option

Save msarchet/3778195 to your computer and use it in GitHub Desktop.
A Javascript Function to position one element around a different element
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'});
}
@msarchet

Copy link
Copy Markdown
Author

Need to out inside vs outside positions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment