Il metodo offsetParent()
restituisce l'antenato più prossimo di un elemento che abbia la proprietà CSS position
impostata su un valore diverso da static
(predefinito). Vediamolo in dettaglio.
Avendo la seguente struttura HTML:
<div id="wrapper">
<div id="inner">
<div id="test"></div>
</div>
</div>
Con i seguenti stili CSS:
#wrapper {
margin: 1em auto;
width: 600px;
position: relative;
border: 1px solid;
}
#inner {
padding: 1em;
}
#test {
width: 100px;
height: 100px;
background: green;
}
Avremo il seguente risultato:
$('#offset').click(function() {
$('#test').offsetParent().addClass('offset');
});