xonegirlz
xonegirlz

Reputation: 8977

scroll view content offset in javascript

Is there a way to find the y content offset from a javascript. The definition of content offset is:

The point at which the origin of the content view is offset from the origin of the scroll view

Upvotes: 0

Views: 1652

Answers (2)

techfoobar
techfoobar

Reputation: 66693

You can use the below code to find the position:

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return curtop;
}

You can call it like findPos(document.getElementById('myID'));

Check this URL for explanation: http://txt.binnyva.com/2007/06/find-elements-position-using-javascript/

Upvotes: 2

Pranav
Pranav

Reputation: 8871

Use offsetLeft and offsetTop to know the X and Y co-ord of an element.

var X=element.offsetLeft;
var Y=element.offsetTop

for more help : https://developer.mozilla.org/en/DOM/element.offsetLeft

Upvotes: 1

Related Questions