 
 # From element and its parents, find the first which we should scroll and which does scroll.
 findScrollableElement = (element, direction, amount, factor) ->
-  while element != document.body and not isScrollableElement element, direction, amount, factor
-    element = DomUtils.getContainingElement(element) ? document.body
+  while element != document.scrollingElement and not isScrollableElement element, direction, amount, factor
+    element = DomUtils.getContainingElement(element) ? document.scrollingElement
   element
 
-# On some pages, document.body is not scrollable.  Here, we search the document for the largest visible
+# On some pages, document.scrollingElement is not scrollable.  Here, we search the document for the largest visible
 # element which does scroll vertically. This is used to initialize activatedElement. See #1358.
-firstScrollableElement = (element=document.body) ->
+firstScrollableElement = (element=document.scrollingElement) ->
   if doesScroll(element, "y", 1, 1) or doesScroll(element, "y", -1, 1)
     element
   else


  # Is element scrollable and not the activated element?
  isScrollableElement: (element) ->
    activatedElement ||= (document.body and firstScrollableElement()) or document.body
    element != activatedElement and isScrollableElement element

  # Scroll the top, bottom, left and right of element into view.  The is used by visual mode to ensure the
  # focus remains visible.
  scrollIntoView: (element) ->
    activatedElement ||= document.body and firstScrollableElement()
    rect = element. getClientRects()?[0]
    if rect?
      # Scroll y axis.

    name = amount
    # the clientSizes of the body are the dimensions of the entire page, but the viewport should only be the
    # part visible through the window
    if name is 'viewSize' and el is document.scrollingElement
      # TODO(smblott) Should we not be returning the width/height of element, here?
      if direction is 'x' then window.innerWidth else window.innerHeight
    else
