简体   繁体   English

在不滚动的情况下将新的Ajax内容加载到当前div之上

[英]Load new Ajax content above current div without scrolling

I'm not sure if I'm allowed to post questions without code here. 我不确定我是否可以在没有代码的情况下发布问题。 Anyway, if it's too broad or not appropriate I'll gladly delete. 无论如何,如果它太宽泛或不合适,我很乐意删除。 I'm currently in the process of implementing pull to refresh, and never ending scroll, both of which are so popular now obviously because of their usability. 我目前正在实施pull to refresh,并且永远不会结束滚动,这两者现在都很受欢迎,显然是因为它们的可用性。

There are also some opponents of these features. 还有一些反对这些功能的人。 I just read an article about how pull-to-refresh should be obsolete from a designer's perspective in a couple years because everything will just automatically refresh, and who wants to have to manually refresh stuff. 我刚刚阅读了一篇文章,关于如何从设计师的角度来看,几年之后应该淘汰即时刷新,因为一切都会自动刷新,谁想要手动刷新东西。 Also the never ending scroll you can lose your place or fail to load new content and have to refresh... 也是永无止境的滚动,你可能会失去你的位置或无法加载新的内容,不得不刷新...

I'd love to have all the new posts come in automatically, and just append them to the top of my list, but if you imagine something like twitter using that, you'd never actually get to read a tweet. 我希望自动将所有新帖子都添加进去,然后将它们添加到我的列表顶部,但是如果您想象使用Twitter的内容,那么您实际上从未真正阅读过推文。 They'd refresh so quickly everything you tried to read would be buried. 他们刷新的速度很快,你试图读的东西都会被埋没。

Has anyone really been far even as decided to use even go want to do look more like? 有没有人真的甚至决定使用甚至想要看起来更像是什么? Sorry, what I mean is, has anyone seen it implemented where new content/posts are automatically loaded above the most recent post but my spot on the page stays the same? 对不起,我的意思是,是否有人看到它实现了新内容/帖子自动加载到最新帖子之上但我在页面上的位置保持不变? I'd like to see how it's done. 我想看看它是如何完成的。 Thank you. 谢谢。

No, I can't remember a site where there's a system exactly like you describe. 不,我不记得有一个系统与你描述的完全一样。 But I made a small function that should mimic what you want to achieve pretty well. 但我做了一个小功能,应该模仿你想要达到的目标。

Here's a demo where you can see the code live. 这是一个演示 ,您可以在其中查看代码。

How the code works 代码如何工作

1). 1)。 Define the variable that keeps track of the current offset to the top of the document, like this: 定义跟踪当前偏移量到文档顶部的变量,如下所示:

var curr_y = $(window).scrollTop();

2). 2)。 Update the value of the current offset to the top of the document every time the user scrolls: 每次用户滚动时,将当前偏移的值更新到文档的顶部:

$(window).scroll(function () {
    curr_y = $(window).scrollTop();
});

3). 3)。 Use your function to fetch data, and then use this code to prepend to the container and then scroll the page to the place where it used to be before the prepend: 使用您的函数获取数据,然后使用此代码预先添加到容器,然后将页面滚动到前置之前的位置:

cont = $('<div>your-content');
$("#container").prepend(cont);
$(window).scrollTop(curr_y + cont.outerHeight(true));

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

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