$.inside
✓ Published0🌍 Public
Aabernier
Last edited Aug 10, 2012
Created on Jul 28, 2012
A jQuery plugin called `$.inside` computes an element's position relative to an ancestor, returning normalized coordinates between 0 and 1 that indicate how far the element extends beyond the ancestor's bounds. The demo uses a draggable and resizable `#bar` inside `#foo`, with the `inside` special event updating two meters and a large text readout in real time at 15 frames per second. The code relies on jQuery UI for drag/resize interactions and the `offsetRelative` helper to calculate offsets.
AI-generated description$.fn
Synopsis
$el.inside( <ancestor> [, <includeMargin>] )
Arguments
<ancestor>: The ancestor to compare $el with
'selector' | jQuery | node
<includeMargin>: Whether to take margins into account
Returns
{
left: <left>
top: <top>
}
<left|top>: Position of $el to <ancestor>
+-----+ +-----+
| 0/0 | | 1/0 |
+-----+ +-----+
+-----------------+
| +-----+ |
| |.5/.5| |
| +-----+ |
+-----------------+
+-----+ +-----+
| 0/1 | | 1/1 |
+-----+ +-----+
Example
Determine if an element is in the viewport — and how far:
$('#foo').inside('html');
$.event.special
Synopsis
$(el).bind('inside', {ancestor: 'html'}, function (e, inside) {
// inside.left
// inside.top
});