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!
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!
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;
}
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
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.