使用window.history.pushState时,jQuery localScroll ajax中断

时间:2013-09-11 13:00:44

标签: jquery ajax scrollto pushstate

我有一个网站,我正在使用ajax加载网站的每个页面,主要是在博客页面和作品集页面之间切换。在对开页面上,我使用localScroll和子导航中的链接。

如果作品集页面是加载的初始页面,这一切都正常。如果首先加载网站上的任何其他页面,并切换到作品集页面,则localScroll不再有效。首先加载作品集页面,切换到另一个页面,没问题。

我已经倒退了,似乎只有在我使用window.history.pushState(更改页面的url)时才会出现问题。如果我删除它,并保留其他所有内容,localScroll将再次运行。

我甚至尝试将window.history.pushState添加到localScroll附带的ajax演示中,它也会在那里发生。

是否有任何理由说明window.history.pushState可能会破坏localScroll,并且无论如何都要让它再次运行?

我在下面添加了我的代码的修剪版本。

$(document).ready(function(){

    $('#nav-subnav').localScroll({
        lazy: true,
        target: scrolltarget
    });

    $(document.body).on('click', '#nav a', function() {
        pageLoad(this);
        return false; 
    });

});

function pageLoad(t){        
    //e.preventDefault();
    var $link = $(t).prop('href');
    if ($link != window.location.href) {
        window.history.pushState({path:$link},'',$link);
        $('#content').add('#nav-submenu').animate({opacity: 0},500, function() {
            $.ajax({
                url: $link,
                success: function(content) {
                    var newTitle = $(content).filter('title').text();
                    document.title = newTitle;
                    $('#nav-subnav').html($(content).find('#nav-subnav .submenu'));
                    $('#content').html($(content).find('#content #wrapper'));
                    $('#content').add('#nav-subnav').animate({opacity: 1},500);
                 }
            }); 
        }); 
    }
    return false; 
};

提前致谢,

0 个答案:

没有答案
相关问题