jquery icon indicating copy to clipboard operation
jquery copied to clipboard

.position() returns an offset from the static element on IE and Edge

Open YojiB opened this issue 6 years ago • 7 comments

The following code is an implementation of .position() from jquery-3.4.1.js.

// Account for the *real* offset parent, which can be the document or its root element
// when a statically positioned element is identified
doc = elem.ownerDocument;
offsetParent = elem.offsetParent || doc.documentElement;
while ( offsetParent &&
	( offsetParent === doc.body || offsetParent === doc.documentElement ) &&
	jQuery.css( offsetParent, "position" ) === "static" ) {

	offsetParent = offsetParent.parentNode;
}

Does the .position() need the parent of root only ? I do not think so. If so, the code should be "if" and not "while".

( offsetParent === doc.body || offsetParent === doc.documentElement )

is not repeat condition, it is break condition I think.

In IE and Edge, if the target element in the table, the .position() returns offset from the table-cell (it's IE and Edge's offsetParent).

I guess the code is intended to be:

	!( offsetParent === doc.body || offsetParent === doc.documentElement ) &&

Note by @mgol: I edited the post to add code formatting. (2019-07-01)

YojiB avatar May 22 '19 11:05 YojiB

Thanks for the report. Can you provide a test case on JS Bin as the issue template requested? Thanks!

Also, please put code snippets in backticks: ```js for the beginning & ``` for the end; that makes code formatted properly.

mgol avatar May 22 '19 12:05 mgol

test case: https://jsbin.com/gisomic/edit?html,css,js,output

Chrome(or FF, Safari) and Edge(or IE) have different results.

I'm sorry and I learned code formatting. I understand that fixing past posts is not a good way. If anyone requests it, I do it.

thank you.

YojiB avatar May 23 '19 01:05 YojiB

I think it's all related to #3479, #3984, #3972, and #3887 but haven't looked to see if there are any unique issues here.

dmethvin avatar May 23 '19 01:05 dmethvin

I knew #3479 but I thought it was different from this report. However, this report is the same as #3887(and others). thank you.

YojiB avatar May 23 '19 02:05 YojiB

Hmm, when I look at the code, the while loop does look odd...

while ( offsetParent &&
	( offsetParent === doc.body || offsetParent === doc.documentElement ) &&
	jQuery.css( offsetParent, "position" ) === "static" ) {

	offsetParent = offsetParent.parentNode;
}

I think the only thing that will be done here is set offsetParent to doc as long as body and html have position: static. I'm not sure that's what we want. This was changed in 1d2df772b4d6e5dbf91df6e75f4a1809f7879ab0.

@gibson042 I must be missing something.

timmywil avatar May 23 '19 13:05 timmywil

This block will be replaced when we address https://github.com/jquery/jquery/issues/3479

timmywil avatar Jul 01 '19 16:07 timmywil

Since #3479 has been scheduled for 3.4.2, I'll update the milestone here as well.

mgol avatar Aug 21 '19 21:08 mgol