Skip to content
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

Merged
merged 3 commits into from
Mar 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions js/src/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,8 @@ const Collapse = (($) => {
}

const dimension = this._getDimension()
const offsetDimension = dimension === Dimension.WIDTH ?
'offsetWidth' : 'offsetHeight'

this._element.style[dimension] = `${this._element[offsetDimension]}px`
this._element.style[dimension] = `${this._element.getBoundingClientRect()[dimension]}px`

Util.reflow(this._element)

Expand Down
2 changes: 1 addition & 1 deletion js/src/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ const Modal = (($) => {
const scrollDiv = document.createElement('div')
scrollDiv.className = ClassName.SCROLLBAR_MEASURER
document.body.appendChild(scrollDiv)
const scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
const scrollbarWidth = scrollDiv.getBoundingClientRect().width - scrollDiv.clientWidth
document.body.removeChild(scrollDiv)
return scrollbarWidth
}
Expand Down
4 changes: 2 additions & 2 deletions js/src/scrollspy.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const ScrollSpy = (($) => {
target = $(targetSelector)[0]
}

if (target && (target.offsetWidth || target.offsetHeight)) {
if (target && (target.getBoundingClientRect().width || target.getBoundingClientRect().height)) {
Copy link
Member

@Johann-S Johann-S Jan 20, 2017

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

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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 !

// todo (fat): remove sketch reliance on jQuery position/offset
return [
$(target)[offsetMethod]().top + offsetBase,
Expand Down Expand Up @@ -198,7 +198,7 @@ const ScrollSpy = (($) => {

_getOffsetHeight() {
return this._scrollElement === window ?
window.innerHeight : this._scrollElement.offsetHeight
window.innerHeight : this._scrollElement.getBoundingClientRect().height
}

_process() {
Expand Down