Skip to content Skip to sidebar Skip to footer

How To Get The Exact Div Positioning Using Jquery/javascript

I am trying to calculate the div positioning. This is my code .

Solution 1:

Since you already have jQuery loading in the page, take a look at the jQuery offset() and position() methods and leverage jQuery selectors.

<script>
    function ReadDivPos(selector) {
        var _divPos = "";

        $(selector).each(function() {
            var p = $(this).offset();
            var w = $(this).width();
            console.log("Top " + p.top) //top
            console.log("left " + p.left) //left
            console.log("right " + p.left + w) //right
            console.log("offsetWidth " + w); //width

            _divPos += "Left " + p.left + ",Width " + w + ",Avail Width " + window.screen.availWidth + ",Right " + (p.left + w) + "\\n";
        });
        return _divPos;
    }

    console.log(ReadDivPos(".content"));
</script>

Post a Comment for "How To Get The Exact Div Positioning Using Jquery/javascript"