Re: [csswg-drafts] [cssom-view] Consider adding Element.scrollParent

This is the type of polyfill I've had to do for `el.scrollRootY`/`el.scrollRootX` more than once.

```js
function scrollRoot(el) {
  var roots = {}
  roots.x = undefined;
  roots.y = undefined;

  function checkScroll(el) {
    if (!el.tagName || el === document.documentElement) {
      roots.x = roots.x || document;
      roots.y = roots.y || document;
      return;
    }
    var parent = el.parentElement;
    var regScroll = /(auto|scroll|overlay)/;

    if (parent.scrollHeight > parent.clientHeight && regScroll.test(window.getComputedStyle(parent).overflowY) && !roots.y) {
      roots.y = parent;
    }
    if (parent.scrollWidth > parent.clientWidth && regScroll.test(window.getComputedStyle(parent).overflowX) && !roots.x) {
      roots.x = parent;
    }
    if (!roots.x || !roots.y) {
      checkScroll(parent);
    }
  }
  checkScroll(el);
  return roots;
}
```
Keep in mind, this doesn't check for a `overflow:hidden` in either axis, which is programmatically scrollable, if not user scrollable. As well as not checking for `position:fixed` which moves an element outside it's scrolling context, or back in, where a `transform` takes the place of it's `initial containing block`. But it could still structurally be inside a nested scrolling element and could then accept scrolling/pointer events for the nested scrolling element? Just saying this stuff get's fun, especially between mouse and touch environments.

And after that, adding scroll listeners, where you still take into account compatibility issues of documentElement and scrollingElement, as well as finding the correct `scrollY`/`scrollX` when the target of the event comes from the document, etc... it's hairy. Personally, I hate having to support scrolling anything besides an actual element (`html` should have been the outermost 'thing' with a scrolling mechanism).

PS @mkay581 @oriadam @upsuper If something like this is spec'd, I don't see the need for "grandparents" or "closest", when one could access the scrollRoot of an elements scrollRoot, all the way up if need be.

-- 
GitHub Notification of comment by jonjohnjohnson
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/1522#issuecomment-378803289 using your GitHub account

Received on Thursday, 5 April 2018 02:34:45 UTC