1

我有一个有点奇怪的问题。我创建了一个 chrome 扩展,当用户编辑谷歌文档时它会自动向下滚动。当文档处于分页模式时,它似乎工作正常。如果我将它切换到紧凑模式,那么 jQuery 似乎不会获得所有请求的元素。基本的 HTML 树在分页和紧凑模式下几乎相同。我很困惑,不知道为什么它不能正常工作。我们应该主要关注“$currentPage”是如何工作的。有时高度在不应该发生时返回 0。

[编辑] 这是它会搞砸的时候:它在分页模式下工作正常。但是,当我在第 2 页上已有文本时以紧凑模式启动扩展程序时,它将按预期工作。如果我将文本一直删除到第 1 页,它仍然会按预期工作。但是,如果我开始在第 2 页输入文本,它就会开始混乱。

这是代码:

function autoScrollFunc(){
   if(autoscrollEnabled){
      var oldCount = pageCount;
      // get all the pages
      var $pages = $("div.kix-page");
      // count how many pages there are
      pageCount = $pages.length;
      console.dir($pages);
      console.dir($pages.last());
      console.dir($pages.last().find("div.kix-paragraphrenderer"));
      $currentPage = $pages.last().find("div.kix-paragraphrenderer").first();
      //console.dir($currentPage);
      //pageHeight = $pages.first().height();
      if(pageCount != oldCount){
         // update data if there are changes in page count
         console.log("Number of pages changed! Count: " + pageCount);
         pageHeight = $pages.first().height();
      }
      // scroll to new position
      scrollToNewPosition();
   }
}

function scrollToNewPosition(){
   if(autoscrollEnabled){
      oldHeight = viewLoc;
      var upperSection = toolbarHeight + getRulerHeight();
      var windowHeight = $(window).height();
      var marginHeight = (pageCount > 1) ? getTopMarginHeight() : firstMarginHeight;
      var docViewHeight = (windowHeight > upperSection) ? (windowHeight - upperSection) : 0;

      // calculate the scroll location
      var totalDocHeight = (pageHeight * (pageCount-1)) + $currentPage.height() + marginHeight;
      viewLoc = (totalDocHeight < docViewHeight) ? 0 : totalDocHeight - docViewHeight + $currentPage.height();

      console.log("upperSection: %s, windowHeight: %s, marginHeight: %s, docViewHeight: %s, totalDocHeight: %s, viewLoc: %s, currentPage.height(): %s",
            upperSection, windowHeight, marginHeight, docViewHeight, totalDocHeight, viewLoc, $currentPage.height());

      if(oldHeight != viewLoc){
         // if there are changes in height, then scroll to the new position
         $docScroller.animate({ scrollTop: viewLoc}, 250);
      }
   }
}

如果您想自己尝试扩展,这里是项目 URL:

http://code.google.com/p/google-docs-autoscroll/

提前谢谢各位。

4

0 回答 0