-
-
Notifications
You must be signed in to change notification settings - Fork 78.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTMLElement.offset* by getBoundingClientRect() #21788
Conversation
@@ -133,7 +133,7 @@ const ScrollSpy = (($) => { | |||
target = $(targetSelector)[0] | |||
} | |||
|
|||
if (target && (target.offsetWidth || target.offsetHeight)) { | |||
if (target && (target.getBoundingClientRect().width || target.getBoundingClientRect().height)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how works the logic of getBoundingClientRect
but you can declare variable to just have one call to getBoundingClientRect
instead of two
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this is what I did at the beginning, but I have to check if target is non null, so the code would have been:
if (target) {
const targetBCR = target.getBoundingClientRect()
if (target.targetBCR.width || targetBCR.height) {
//Do something
}
}
So that makes longer code and less legible IMO. That said, I don't know if the browser has to compute the result for each call or if it just return a result that was already there.
I'll do some research quickly to check and I'll change the code accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found something about the algorithm if it can help you : https://drafts.csswg.org/cssom-view/#dom-element-getboundingclientrect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the chance. Thanks !
Fixes #19774.