简体   繁体   English

防止DIV滚动DOM内容更改

[英]Prevent DIV from scrolling on DOM content change

I am writing a document reading application using Node-Webkit. 我正在使用Node-Webkit编写文档阅读应用程序。 A document can be hundreds of pages long and the interface allows opening the document to a specific chapter. 一个文档可以长达数百页,并且该界面允许将文档打开到特定章节。 Once the initial chapter is displayed, I want to load the rest of the book asynchronously. 显示初始章节后,我想异步加载本书的其余部分。 Since I don't know what direction the reader will scroll, my strategy is to alternately load chapters before and then after the initial chapter: 由于我不知道读者将朝哪个方向滚动,因此我的策略是在初始章节之前和之后交替加载章节:

load chapter x -> load chapter x-1 -> load chapter x+1 -> load x-2 -> load x+2 ... 加载第x章->加载第x-1章->加载第x + 1章->加载x-2->加载x + 2 ...

I am displaying the book in a containing div element with each chapter being a div within the container. 我将本书显示在一个包含div元素的容器中,每一章都是一个div容器。 I'm using jquery .before() and .after() to insert each chapter as it is fetched. 我正在使用jquery .before()和.after()在获取每个章节时插入它。

My problem is that the browser automatically scrolls up when I add chapters earlier in the book. 我的问题是,当我在本书的前面添加章节时,浏览器会自动向上滚动。 So if I start with chapter 10, the browser scrolls up when I add chapter 9 and again with chapter 8 and so forth. 因此,如果我从第10章开始,当我添加第9章时,浏览器会向上滚动,然后在添加第8章时,浏览器会向上滚动,依此类推。

One (unsatisfactory) solution to the problem is to make an anchor tag for each chapter and store the id of the first chapter loaded. 解决该问题的一种方法(不令人满意)是为每个章节创建一个定位标记,并存储所加载的第一章的ID。 Then after each chapter is fetched, runing the code: 然后在获取每章之后,运行代码:

window.location.href = #originalChapter

keeps the initial chapter in the browser viewport. 将初始章节保留在浏览器视口中。 Of course the problem with that is that the reader cannot scroll while the rest of the book is being loaded. 当然,这样做的问题是,在加载本书的其余部分时,读者无法滚动。

Ideally, I would like to disable scrolling, load a chapter and then re-enable until the next chapter is fetched. 理想情况下,我想禁用滚动,加载一章,然后重新启用,直到获取下一章。 I'm so far unable to figure out how to do that. 到目前为止,我还无法弄清楚该如何做。

The simple solution in my mind would be to not have the elements in the DOM until they are loaded. 我认为简单的解决方案是在元素被加载之前不将其包含在DOM中。 The user won't be able to scroll because there won't be content to scroll to. 用户将无法滚动,因为将没有内容可以滚动到。 Then, it's just a matter of preserving the viewport when the elements are added. 然后,只需在添加元素时保留视口即可。

Take the initial position of your scroll container and the height of the overflow container: 取得滚动容器的初始位置和溢出容器的高度:

var scrollTop = $scroll.scrollTop(),
    height    = $container.height();

and use them to preserve the viewport when you prepend new elements. 并使用它们,当你保存视口prepend新元素。 You don't have to do anything when you're doing the append operation. 在执行append操作时,您无需执行任何操作。

var newHeight    = $container.height(),
    newScrollTop = scrollTop + newHeight - height;
$scroll.scrollTop(newScrollTop);

Here's a quick example: http://jsfiddle.net/Wexcode/tfszaocz/ 这是一个简单的示例: http : //jsfiddle.net/Wexcode/tfszaocz/

如果要通过锚点( <a/> )的点击处理程序更新DOM,请确保从回调函数返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM