I’m trying to use jQuery and jQuery UI to drag a <ul> element horizontally located inside a <div>.
I’d like to get the coordinates of the buttons located inside the <li> elements to center them inside #the_parent, but the position I get changes every time I resize the browser window, how I can get a position relative to #the_parent?
<script type:"text/javascript">
$(document).ready (function () {
$(".button").click (function () {
var offset = $(this).offset();
alert (offset.left);
});
});
</script>
<style type="text/css">
#dragme li { float:left; list-style:none; }
#the_parent { width:100%; }
.spacer { width:100px; }
.button div { padding:5px; background-color:darkgrey; color:white; }
</style>
<!-- this is contained inside another div of width:600px and it's centered in the middle of the page -->
<div id="the_parent">
<ul id="dragme">
<li class="spacer"></li>
<li id="btn_01" class="button">
<div>click</div>
</li>
<li class="spacer"></li>
<li id="btn_02" class="button">
<div>click</div>
</li>
<li class="spacer"></li>
</ul>
</div>
Looks like you want
position()instead ofoffset(). From the jQuery docs: