PHP Ajax动态列表的后退操作——保存列表内容和点击位置

4 篇文章 0 订阅

需求

列表页是ajax动态加载的,每次点击进入查看后返回,会丢失之前的位置,并且列表需要重新加载一次。

思路

绑定点击事件,点击的时候获取当前位置和列表HTML,存入sessionStorage。
后退的时候要判断是否从前一个页面返回的,这里就用到 **history.replaceState**

代码

//绑定事件
$('#list a').live('click',function(e){
    history.replaceState({}, document.title, location.href+"&history=-1");//写入标志位
    var _scrollTop = $("body").scrollTop();
    var _bodyHeight = $("body").height();
    sessionStorage.setItem('list',$('#list').html());
    sessionStorage.setItem('scrolltop',_scrollTop);
    sessionStorage.setItem('bodyheight ',_bodyHeight);
});

if(window.location.search.indexOf('history=-1') > 0)//检测标志位
{
    $('#list').html(sessionStorage.getItem('list'));
    $("body").height(sessionStorage.getItem('bodyheight'));
    $("body").scrollTop(sessionStorage.getItem('scrolltop'));
    history.replaceState({}, document.title, location.href.replace('&history=-1',''));//还原真实路径
}
else
{
    //不是后退的则加载
    ajaxdata(1);
}

参考

  1. ajax与HTML5 history pushState/replaceState实例 « 张鑫旭-鑫空间-鑫生活
  2. pjax
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值