最近在學(xué)javascript,才慢慢開始讀懂JS代碼。對HTML前臺AJAX加載技術(shù)研究了一番,像博客站或者圖片站運用AJAX異步無限加載,做成瀑布流,提高用戶體驗,這是AJAX異步加載的優(yōu)點。
AJAX異步加載的弊端:屬于WEB前端技術(shù),蜘蛛不會抓JS調(diào)用的內(nèi)容,也就是并不利于SEO優(yōu)化。所以要運用這個功能的話最好有一部分直接調(diào)用,另一部分采用AJAX異步加載。
接下來用織夢來做AJAX異步加載WEB前端開發(fā)。網(wǎng)上有諸多教程,經(jīng)過筆者整理,給出詳細(xì)說明,修正后分享給大家。
打開/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;//每頁多少條,也就是一次加載多少條數(shù)據(jù) $start = $page>0 ? ($page-1)*$pagesize : 0;//數(shù)據(jù)獲取的起始位置。即limit條件的第一個參數(shù)。 $typesql = $typeid ? " WHERE typeid=$typeid" : '';//這個是用于首頁實現(xiàn)瀑布流加載,因為首頁加載數(shù)據(jù)是無需分類的,所以要加以判斷,如果無需 $total_sql = "SELECT COUNT(id) as num FROM `dede_archives` $typesql "; $temp = $dsql->GetOne($total_sql); $total = 0;//數(shù)據(jù)總數(shù) $load_num =0; if(is_array($temp)){ $load_num= round(($temp['num']-15)/$pagesize);//要加載的次數(shù),因為默認(rèn)已經(jīng)加載了 $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"; //echo "$sql"; $dsql->SetQuery($sql); $dsql->Execute('list'); $statu = 0;//是否有數(shù)據(jù),默認(rèn)沒有數(shù)據(jù) $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'];//完整的標(biāo)題 $row['shorttitle'] = $row['shorttitle'];//副標(biāo)題 $row['title'] = cn_substr($row['title'], 60);//截取后的標(biāo)題 $data[$index] = $row; $index++; } if(!empty($data)){ $statu = 1;//有數(shù)據(jù) } $result =array('statu'=>$statu,'list'=>$data,'total'=>$total,'load_num'=>$load_num); echo json_encode($result);//返回數(shù)據(jù) exit(); }
模板文件增加內(nèi)容
在需要用AJAX異步加載的模板的</head>前增加代碼
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script type="text/javascript"> var loadConfig = { url_api:'{dede:global.cfg_cmspath/}/plus/list.php', //修正plus目錄 typeid:{dede:field name='typeid'/}, //首頁此處是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('<li>'); arr.push('<a href="'+data[i].arcurl+'" class="preview">'); arr.push('<img class="" width="" height="" alt="data[i].title" src="'+data[i].picname+'" />'); arr.push('</a>'); arr.push('[<b>'+data[i].typelink+'</b>]'); arr.push('<a href="'+data[i].arcurl+'" class="title">'+data[i].title+'</a>'); arr.push('</li>'); } $('.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>
首頁HTML代碼示例
注意:首頁模板用arclist標(biāo)簽,用row='3'是初始化調(diào)用的3條;列表模板用list標(biāo)簽,用pagesize='3'。模板里面的<p class="data-list">與js里面的data-list對應(yīng),需要把循環(huán)部分全部包裹起來
<p class="listbox"> <ul class="e2"> <p class="data-list"> {dede:arclist row='3'} <li> [field:array runphp='yes']@me = (empty(@me['litpic']) ? "" : "<a href='{@me['arcurl']}' class='preview'><img src='{@me['litpic']}'/></a>"); [/field:array] [<b>[field:typelink/]</b>] <a href="[field:arcurl/]" class="title">[field:title/]</a> <span class="info"> <small>日期:</small>[field:pubdate function="GetDateTimeMK(@me)"/] <small>點擊:</small>[field:click/] <small>好評:</small>[field:scores/] </span> <p class="intro"> [field:description/]... </p> </li> {/dede:arclist} </p><!--data-list--> </ul> </p> <!-- /listbox -->
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持素材無憂。
版權(quán)聲明: 本站資源均來自互聯(lián)網(wǎng)或會員發(fā)布,如果侵犯了您的權(quán)益請與我們聯(lián)系,我們將在24小時內(nèi)刪除!謝謝!
轉(zhuǎn)載請注明: 詳解織夢模板DEDE首頁列表頁AJAX無限加載瀑布流修正版