Getting the position of a floating element

I have a code like this:

<style>
.box {
    width: 100px;
    height: 100px;
    margin: 10px;
    background: green;
    border: 1px solid black;
    float: left;

}

#wrapper {
    position: relative;
}
</style>


<div id="wrapper">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>

I need to get the top left of each field relative to #wrapper. I am trying to do this through jQuery.position (), however, I am not getting the correct results. Keep getting 0.0. Maybe someone else. I think the problem here is that we are swimming ... if they were absolutely positioned, I would read them correctly.

+3
source share
4 answers

Here is some jQuery code that will warn the left position. It works with a floating well.

$(function() {
    $('#wrapper div').each( function( index, item ) {
            alert( $(this).position().left);
    });
});

here's jsFiddle: http://jsfiddle.net/ytGYS/

+6
source

, , #wrapper , . , , , #wrapper , jQuery , .

+4

+1 andrewheins:

; #wrapper , , $(). offset() .

, $(). offset() , #wrapper . , $(document).ready(), . ( (), )

. , .

0

All Articles