As per my previous comment, I still think this is not necessary. But if it is, I think you can split this into two parts, then you only need to call the first part here. Pseudocode:

function isElementOutsideBody(element) {
  ...
}

function elementIsOutsideScrollArea(element) {
  if (isElementOutsideBody(element))
    return false;

  for each parent:
    if element is outside parent
      if (isElementOutsideBody(element))
        continue
      ...
}

That make sense?