Normally when getting the offset using jQuery I do offset().left. My understanding is that offsetLeft is pure javascript, so why not use this over the jQuery offset().left? Does it really help anything cross-browser? or?
And when using offset().left i’ve seen people take out the () so it’s just offset.left, is this okay to do? Is it only okay to do if there’s nothing in the parenthesis?
As a side note: I’ve heard that in IE7 when using the jQuery way of offset().top it gives a screwey offset based on how much you’ve scrolled. Someone in the comments on the jQuery API page for offset() said document.getElementById(anchor).offsetTop fixed it.
Getting offsets can be a little tricky in some browsers.
offsetTopand-Leftare incorrect in IE 6 and below and others likeoffsetXandoffsetYare completely inconsistent across browsers (see Quirksmode)Generally, when a library includes some functionality even when the same functionality seems to already exist, it’s usually done for cross-browser purposes.
As far as I can tell, using
offset.leftinstead ofoffset().leftshouldn’t even work. The latter would be “get theleftproperty of the object returned by theoffset()function”, which is what you’d want. The former, with no parentheses, would be “get theleftproperty of the functionoffset“, which doesn’t make much sense to me (unless the object whoseoffsetproperty you’re accessing isn’t a jQuery wrapper, in which case theoffsetproperty might not be a function, but instead an object withleftandtopproperties).But I’m not a jQuery expert