tags:

views:

51

answers:

3

I'd like to determine the # of pixels between my DIV and my monitor.

Is it something like scrollLeft()? ..but I don't think so.

Please help, thanks!

A: 

I'm not sure if this can be done only using jQuery, however it can be done using just javascript:

function getLeftPosition(obj){
    var leftValue= 0;
    while(obj){
    leftValue+= obj.offsetLeft;
    obj= obj.offsetParent;
    }
    return leftValue;
}
James
Thanks. I'm new to javascript. if I have a div called #frame, what do i pass to that function? How do I call it
TIMEX
try returning {left:leftValue, top:topValue} instead
Marius
@alex - You pass the object that you want to check, e.g. getPosition(document.getElementById("frame"))
James
+1  A: 

offsetLeft gets the offset to the left from the parent element (offsetParent). If you call the following function you will get the distance from the left of the document. Then you can subtract scrollLeft from that value to find the position from the left side of the window.

function getLeftPos(elm){
  var left = 0;
  while(elm){
    left += elm.offsetLeft;
    elm= elm.offsetParent;
  }
  return left;
}
getLeftPos(elm) - document.scrollLeft
Marius
+5  A: 

jQuery provides a method called offset(), which returns an object {left: ..., top: ...}. To get the X position of a DIV relative to the document's left edge, you could call

$("#mydiv").offset().left

You can find the offset method's documentation here.

AKX
Thanks, this was the best answer!
TIMEX