视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
单页面情况下laypage开启时hash的优化方法(代码)
2020-11-27 19:31:52 责编:小采
文档


本篇文章给大家带来的内容是关于单页面情况下laypage开启时hash的优化方法(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

单页面的时候,laypage开启hash会冲掉之前的hash

解决思路,调整hash的跳转方式以及curr的计算方式

之后的hash会是这样 #/data/tiebaluntan#!page=98

修改地方有两个:

laypage.js中

//渲染分页
 Class.prototype.render = function(load){
 var that = this
 ,config = that.config
 ,type = that.type()
 ,view = that.view();
 
 if(type === 2){
 config.elem && (config.elem.innerHTML = view);
 } else if(type === 3){
 config.elem.html(view);
 } else {
 if(doc[id](config.elem)){
 doc[id](config.elem).innerHTML = view;
 }
 }
 config.jump && config.jump(config, load);
 
 var elem = doc[id]('layui-laypage-' + config.index);
 that.jump(elem);
 
 if(config.hash && !load){
 // location.hash = '!'+ config.hash +'='+ config.curr; // 屏蔽掉该行,修改为下一行
 location.hash = location.hash.substr(0,location.hash.indexOf("#!")==-1 ? 99999:location.hash.indexOf("#!")) + '#!'+ config.hash +'='+ config.curr;
 }
 
 that.skip(elem);
 };

另外就是page的渲染中的curr计算,不能根据官方的教程,需要按照如下:

//开启HASH
 laypage.render({
 elem: 'test-laypage-demo5'
 ,count: 500
 //,curr: location.hash.replace('#!fenye=', '') //获取hash值为fenye的当前页
 ,curr: location.hash.indexOf("#!")==-1 ? "" : location.hash.substr(location.hash.indexOf("#!"),).replace('#!fenye=', '')//获取hash值为fenye的当前页
 ,hash: 'fenye' //自定义hash值
 });

下载本文
显示全文
专题