简体   繁体   English

自动刷新Div,而无需滚动到Div的顶部

[英]Auto refresh a Div without scrolling to the top of Div

I have a script that refreshes a Div on my website every 20 seconds. 我有一个脚本,每20秒刷新一次我网站上的Div。 The problem is, once it refreshes, it scrolls to the to of the Div. 问题是,一旦刷新,它将滚动到Div的。 I want it to stay at the last position and not scroll to the top after a refresh. 我希望它保持在最后一个位置,而不是刷新后滚动到顶部。 Could someone please look at this script and maybe point out what I'm doing wrong? 有人可以看看这个脚本,也许指出我做错了什么? Thanks in advance! 提前致谢!

var time = new Date().getTime();

var refreshTime = 20*1000;

$(document).bind("blur focus focusin focusout load resize scroll unload click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup error hover change", function(e) {
    time = new Date().getTime(); 
});

var lastScrollPos = 0;

$('#feed1').on('scroll', function() {
    lastScrollPos = this.scrollTop;
});

function refresh() {

    if(new Date().getTime() - time >= refreshTime)

        $('#feed1').load(location.href="/dashboard" , function () {
            $(this).scrollTop(lastScrollPos);
        });

    else

        setTimeout(refresh, refreshTime);

}

setTimeout(refresh, refreshTime);

If you do location.href="/dashboard" , the browser will discard all the page's state (including scripting variables) and load "/dashboard" (assigning a value to location.href is identical to calling location.assign("/dashboard") ). 如果执行location.href="/dashboard" ,浏览器将丢弃页面的所有状态(包括脚本变量)并加载“ / dashboard”(将值分配给location.href与调用location.assign("/dashboard") )。

The jQuery load function will probably not even require you to reposition the scroll offset, if you gave it chance to execute! 如果您有机会执行, jQuery加载功能甚至可能甚至不需要您重新定位滚动偏移!

Try: 尝试:

$('#feed1').load("/dashboard #feed1");

UPDATE: 更新:

It seems the HTTP request fired by jQuery's load mechanism is receiving a truncated response in your case (content-length: 0). 在您的情况下,似乎jQuery的加载机制触发的HTTP请求正在收到截断的响应(内容长度:0)。

You would have to put the scroll position you want to keep into localStorage. 您必须将要保留的滚动位置放入localStorage。 Try: 尝试:

var time = new Date().getTime();

var refreshTime = 20*1000;

$(document).bind("blur focus focusin focusout load resize scroll unload click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup error hover change", function(e) {
    time = new Date().getTime(); 
});

var previousScrollPos = localStorage.getItem("lastScrollPos");
if(previousScrollPos)
{
    $('#feed1').scrollTop(previousScrollPos);
    localStorage.removeItem("lastScrollPos");
}

function refresh() {

    if(new Date().getTime() - time >= refreshTime)
    {
        localStorage.setItem("lastScrollPos", $('#feed1').scrollTop());
        location.reload();
    }

}

setInterval(refresh, refreshTime);

I'm not 100% sure that you want to bind to the scroll event of #feed1 or just of body, I haven't seen your page. 我不确定100%确定要绑定到#feed1滚动事件还是仅绑定到body滚动事件,我还没有看到您的页面。 If anybody clicks "load more" on the news feed, those additional items will be hidden again since you're reloading the page. 如果有人在新闻源上单击“加载更多”,则由于您正在重新加载页面,这些其他项将再次被隐藏。 Not ideal at all. 一点都不理想。

Many users will hate having the page periodically reload, it's also an accessibility failure since this will interfere with screen reading software. 许多用户讨厌定期重新加载页面,这也是可访问性失败,因为这会干扰屏幕阅读软件。 It might be a simpler idea to just put a refresh newsfeed icon on the page, which just reloads the whole page, when the user wants to (instead of periodically). 将刷新新闻源图标放在页面上可能是一个更简单的想法,该图标将在用户需要时(而不是定期地)重新加载整个页面。

Why don't you use feed1 div only for loading your dashboard contents and handle its position using style. 为什么不只使用feed1 div加载仪表板内容并使用样式处理其位置。

#feed1 {
height: 150px;
overflow: auto;

} }

Add other data outside feed1 div because load method will overwrite feed1's content. feed1 div之外添加其他数据,因为加载方法将覆盖feed1的内容。

See this example if you are looking for something similar otherwise you can modify this example so that other can understand your requirement/scenario. 如果您正在寻找类似的东西,请参见此示例 ,否则可以修改此示例,以便其他人可以了解您的需求/方案。

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

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