很多用織夢dedecms做圖片類的網站都需要用到無限加載這個功能,這個功能需要用到ajax,接下來吾愛編程就為大家介紹一下如何實現瀑布流無限加載。
第一步:找到並打開/pluss/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 `tufei_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 `tufei_archives` as a JOIN `tufei_arctype` AS t ON a.typeid=t.id $typesql ORDER BY id DESC LIMIT $start,$pagesize";
//echo "$sql";
$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['typelink'] = "<a href='".$row['typeurl']."'>".$row['typename']."</a>";//分類鏈
$row['fulltitle'] = $row['title'];//完整的標題
$row['shorttitle'] = $row['shorttitle'];//副標題
$row['title'] = cn_substr($row['title'], 60);//截取後的標題
$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();
}
第二步:在需要使用無線加載的模板裡引用下面這個js代碼:
<script src=
"/js/jquery.min.js"
></script>
並在底部添加如下代碼:
<script type="text/javascript">
var loadConfig = {
url_api:'/pluss/list.php',
typeid:0,
page:2,
pagesize:3,
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('<a href="'+data[i].arcurl+'">');
arr.push('<div class="item">');
arr.push('<div class="thumbnail">');
arr.push('<img class="img-responsive" src="'+data[i].picname+'" width=460 height=255 />');
arr.push('</div>');
arr.push('<div class="caption">');
arr.push('<h3>'+data[i].title+'</h3>');
arr.push('<div class="place">'+data[i].shorttitle+'</div>');
arr.push('</div>');
arr.push('</div>');
arr.push('</a>');
}
$('.data-list').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();
</script>
上面的代碼中的”$('.data-list').append(arr.join(''));“裡的”data-list“對應模板內列表的外框class屬性。
arr.push部分對應的是列表代碼(裡面的內容可以根據自己的需要改成相對應的即可),
如果在列表頁只需把代碼中的”typeid:0,“ 修改為”typeid:{dede:field name="typeid"/},“即可。
*