您现在的位置是:主页 > 织梦教程 > 织梦教程

织梦列表页无限下拉加载内容或点击加载再出现内容

织梦BUG 2019-07-19 【织梦教程】 人已围观

简介提示:该功能为列表页下拉加载内容,首页暂时无法使用。 一、首先找到并打开/plus/list.php文件,在里面找到如下代码: require_once(dirname(__FILE__)."/../include/common.inc.php"); 在其代码下面添...

提示:该功能为列表页下拉加载内容,首页暂时无法使用。

一、首先找到并打开/plus/list.php文件,在里面找到如下代码:

require_once(dirname(__FILE__)."/../include/common.inc.php");

在其代码下面添加如下代码:

//列表页瀑布流无限加载代码
if(isset($_GET['ajax'])){
$typeid = isset($_GET['typeid']) ? intval($_GET['typeid']): 0;//传递过来的分类ID
$page = isset($_GET['page']) ? intval($_GET['page']): 0;//页码
$pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']): 15;//每页多少条,也就是一次加载多少条数据
$start = $page>0 ? ($page-1)*$pagesize : 0;//数据获取的起始位置。即limit条件的第一个参数。
$typesql = $typeid ? " WHERE typeid=$typeid" : '';//这个是用于首页实现瀑布流加载,因为首页加载数据是无需分类的,所以要加以判断,如果无需
$total_sql = "SELECT COUNT(id) as num FROM `dede_archives` $typesql ";
$temp = $dsql->GetOne($total_sql);
$total = 0;//数据总数
$load_num =0;
if(is_array($temp)){
$load_num= round(($temp['num']-15)/$pagesize);//要加载的次数,因为默认已经加载了
$total = $temp['num'];
}
$sql = "SELECT a.*,t.typedir,t.typename,t.isdefault,t.defaultname,t.namerule,
t.namerule2,t.ispart, t.moresite,t.siteurl,t.sitepath
FROM `dede_archives` as a JOIN `dede_arctype` AS t ON a.typeid=t.id $typesql ORDER BY id DESC LIMIT $start,$pagesize";
$dsql->SetQuery($sql);
$dsql->Execute('list');
$statu = 0;//是否有数据,默认没有数据
$data = array();
$index = 0;
while($row = $dsql->GetArray("list"))
{
$row['info'] = $row['info'] = $row['infos'] = cn_substr($row['description'],160);
$row['id'] =  $row['id'];
$row['filename'] = $row['arcurl'] = GetFileUrl($row['id'],
$row['typeid'],$row['senddate'],$row['title'],$row['ismake'],
$row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],
$row['filename'],$row['moresite'],$row['siteurl'],$row['sitepath']);
$row['typeurl'] = GetTypeUrl($row['typeid'],$row['typedir'],
$row['isdefault'],$row['defaultname'],$row['ispart'],
$row['namerule2'],$row['moresite'],$row['siteurl'],$row['sitepath']);
if($row['litpic'] == '-' || $row['litpic'] == '')
{
$row['litpic'] = $GLOBALS['cfg_cmspath'].'/images/defaultpic.gif';
}
if(!preg_match("#^http://#i", $row['litpic']) &&$GLOBALS['cfg_multi_site'] == 'Y')
{
$row['litpic'] = $GLOBALS['cfg_mainsite'].$row['litpic'];
}
$row['picname'] = $row['litpic'];//缩略图
//$row['stime'] = GetDateMK($row['pubdate']);
$row['stime'] = date('Y-m-d H:i', $row['pubdate']);
$row['click'] = $row['click'];
$row['typelink'] = "".$row['typename']."";//分类链
$row['fulltitle'] = $row['title'];//完整的标题
$row['shorttitle'] = $row['shorttitle'];//副标题
$row['title'] = cn_substr($row['title'], 80);//截取后的标题
$row['url'] = $row['arcurl'];
$data[$index] = $row;
$index++;
}
if(!empty($data)){
$statu = 1;//有数据
}
$result =array('statu'=>$statu,'list'=>$data,'total'=>$total,'load_num'=>$load_num);
echo json_encode($result);//返回数据
exit();
}

列表页html模板添加如下代码:

{dede:arclist row='3'}
<div class="listpic" id="listpic">
<li class="post"><a href="[field:arcurl/]">[field:title/]</a></li>
</div>
{/dede:arclist}
<script type="text/javascript">
var loadConfig = {
url_api:'/plus/list.php',
typeid:{dede:field name="crossid"/},
page:2,
pagesize:6,  //这个就是滑动一次添加几条信息的参数设置
loading : 0,
}
function  loadMoreApply(){
if(loadConfig.loading == 0){
var typeid = loadConfig.typeid;
var page = loadConfig.page;
var pagesize = loadConfig.pagesize;
var url = loadConfig.url_api,data={ajax:'pullload',typeid:typeid,page:page,pagesize:pagesize};
var sTop = document.body.scrollTop || document.documentElement.scrollTop, dHeight = $(document).height(), cHeight = document.documentElement.clientHeight;
console.log(dHeight);
if (sTop + cHeight >= dHeight - cHeight) {
loadConfig.loading = 1;
function ajax(url, data) {
$.ajax({url: url,data: data,async: false,type: 'GET',dataType: 'json',success: function(data) {
addContent(data);
}});
}
ajax(url,data);
}
}
}
function addContent (rs){
if(rs.statu== 1){
var data = rs.list;
var total = rs.total;
var arr=[];
var length = data.length;
for(var i=0;i<length;i++){
arr.push('<li class="post">');
arr.push('<a href="'+data[i].url+'">');
arr.push(data[i].fulltitle);
arr.push('</a>');
arr.push('</li>');
}
$('.listpic').append(arr.join(''));
loadConfig.load_num = rs.load_num;
if(total<loadConfig.page*loadConfig.pagesize || loadConfig.page > loadConfig.load_num){
window.removeEventListener('srcoll',loadMoreApply,false);
}
loadConfig.page++;
loadConfig.loading = 0;
}
}
function pullLoad(){
window.addEventListener('scroll', loadMoreApply, false);
}
pullLoad()
checkMobile();
</script>

这样网站就实现了下拉加载功能。

如果想修改为点击加载才加载。

将页面中使用的js代码里的loadMoreApply函数修改为以下代码:

function loadMoreApply() {
if (loadConfig.loading == 0) {
var typeid = loadConfig.typeid;
var page = loadConfig.page;
var pagesize = loadConfig.pagesize;
var url = loadConfig.url_api,
data = {
ajax: 'pullload',
typeid: typeid,
page: page,
pagesize: pagesize
};
loadConfig.loading = 1;
function ajax(url, data) {
$.ajax({
url: url,
data: data,
async: false,
type: 'GET',
dataType: 'json',
success: function(data) {
addContent(data);
}
});
}
ajax(url, data);
}
}

然后在页面中添加个按钮

<input type="button" name="a" value="加载" onclick="loadMoreApply()">


-END-

浏览完了?你可以 点我去评论留下观点!

站点信息

  • 建站时间:2018-10-24
  • 标签管理标签云
  • 网站地图XML网站地图
  • 文章统计:69篇文章
  • 站长微信:微信二维码