jQuery+pjax簡(jiǎn)單示例匯總
pjax 是一個(gè)jQuery插件,它使用 ajax 和 pushState 來(lái)實(shí)現(xiàn)快速的瀏覽體驗(yàn),包括真正的固定鏈接,頁(yè)面標(biāo)題和工作返回按鈕。
ajax缺點(diǎn)是破壞了瀏覽器的前進(jìn)后退,因?yàn)閍jax的請(qǐng)求不會(huì)留在歷史記錄中。pjax就不一樣了,pjax被解釋成ajax+pushState的封裝,因?yàn)樗補(bǔ)jax的請(qǐng)求寫(xiě)入歷史記錄,并反映在地址欄,這樣用戶就能愉快地使用前進(jìn)后退了。pjax有好幾個(gè)實(shí)現(xiàn)方法,這里使用最常用的jQuery庫(kù),使用jquery.pjax.js。演示代碼的服務(wù)器端使用PHP腳本語(yǔ)言。
Pjax用在那兒?就說(shuō)百度云盤(pán)吧,這個(gè)大家肯定都用過(guò)。百度云盤(pán)PC端,在點(diǎn)擊打開(kāi)某個(gè)文件夾后會(huì)打開(kāi)這個(gè)文件夾下的文件,其實(shí)顯示文件的這個(gè)div就用到了pjax技術(shù)。地址欄變換,內(nèi)容更換,但是卻是一個(gè)ajax請(qǐng)求。等到后退的時(shí)候,不必重新請(qǐng)求上一層文件夾的內(nèi)容,因?yàn)槭谴嬖谠跉v史記錄中的。而且,開(kāi)發(fā)者還可以選擇時(shí)候使用cache和storage緩存。
示例一、
<!DOCTYPE html> <html> <head> <title>pjax</title> <meta charset="utf-8"> </head> <body> <h1>My Site</h1> <div> Go to <a href="res1.php" rel="external nofollow" >第一頁(yè)</a>.<a href="res2.php" rel="external nofollow" >第二頁(yè)</a> </div> <div id="container"></div> </body> <script src="../jquery-2.1.4.min.js"></script> <script src="../jquery.pjax.js"></script> <script type="text/javascript"> $(document).pjax('a', '#container') </script> </html>
res1.php
<?php echo "<div style='background:red;'>第一頁(yè)</div>";
res2.php
<?php echo "<div style='background:red;'>第二頁(yè)</div>";
解釋?zhuān)?/p>
$(document).pjax('a', '#Container')
其中 a 是觸發(fā)元素, #container 是裝載 pjax 返回內(nèi)容的容器,下面也是這樣。
示例二、
<!DOCTYPE html> <html> <head> <title>pjax</title> <meta charset="utf-8"> </head> <body> <h1>My Site</h1> <div> <input type="button" id="clickMe" value="GO"> </div> <div id="container"></div> </body> <script src="../jquery-2.1.4.min.js"></script> <script src="../jquery.pjax.js"></script> <script type="text/javascript"> $(function(){ $('#clickMe').click(function(){ $.pjax({ url: './res3.php', container: '#container' }); }); }); </script> </html>
服務(wù)器端代碼:
res3.php:
<?php echo "<div style='background:red;'>第三頁(yè)</div>";
三綜合應(yīng)用
window.history.pushState(state, title, url);
// https://developer.mozilla.org/zh-CN/docs/Web/API/History/pushState
// @狀態(tài)對(duì)象: 記錄歷史記錄點(diǎn)的額外對(duì)象,可以為空.
// @頁(yè)面標(biāo)題: 目前所有瀏覽器都不支持.
// @可選的URL: 瀏覽器不會(huì)檢查URL是否存在,只改變URL.URL必須同域,不能跨域.
PJAX其實(shí)就是HTML5 window.history.pushState(state, title, url)這個(gè)新的API加上傳統(tǒng)的AJAX技術(shù),一般用來(lái)實(shí)現(xiàn)無(wú)刷新的頁(yè)面加載.pushState的作用主要是:改變URL和添加返回歷史.這樣AJAX無(wú)刷新加載頁(yè)面后,用戶還可以正常進(jìn)行后退和前進(jìn),JS的window.history.back()和window.history.forward()也能正常工作.下面就是一個(gè)用pushState + jQuery AJAX實(shí)現(xiàn)的無(wú)刷新的頁(yè)面加載,不支持的瀏覽器則自動(dòng)退化成打開(kāi)原始的鏈接打開(kāi)形式.
index.php:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Index</title> <script src="jquery.js"></script> </head> <body> <div id="main"> <a href="data.php" rel="external nofollow" >data.php</a> <script> $(document).ready(function() { $('#main').on('click','a',function(e) { if(window.history.pushState) { e.preventDefault(); //不跟隨原鏈接跳轉(zhuǎn) url = $(this).attr('href'); $.ajax({ async: true, type: 'GET', url: 'data.php', data: 'pjax=1', success: function(data) { window.history.pushState(null, null, url); //改變URL和添加返回歷史 document.title = data.title; //設(shè)置標(biāo)題 $('#main').html(data.main); //設(shè)置內(nèi)容 } }); } else { return; //低版本IE8等不支持HTML5 pushState,直接返回進(jìn)行鏈接跳轉(zhuǎn) } }); }); </script> </div> </body> </html>
data.php:
<?php if(isset($_GET['pjax'])) { //PJAX請(qǐng)求返回JSON $arr['title'] = 'Data'; $arr['main'] = '<h1>Data Content</h1>'; //下面這兩句是把PHP數(shù)組轉(zhuǎn)成JSON對(duì)象返回 header('Content-Type: application/json; charset=utf-8'); echo json_encode($arr); } else { //常規(guī)請(qǐng)求返回HTML ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Data</title> <script src="jquery.js"></script> </head> <body> <div id="main"><h1>Data Content</h1></div> </body> </html> <?php } ?>
注意,JS統(tǒng)計(jì)代碼應(yīng)該放到main塊里面才能正常統(tǒng)計(jì)頁(yè)面訪問(wèn)數(shù).
示例二:
<div class="body"> <?php $action_name = $Think.ACTION_NAME; ?> <!-- 頭部喲 --> <?php if ($action_name == 'news'): ?> <include file="Brand:header_news" /> <?php elseif ($action_name == 'forum'): ?> <include file="Brand:header_forum" /> <?php endif; ?> <!-- 資訊的二級(jí)分類(lèi) --> <div class="cb"></div> <div class="brand-news-nav pjax"> <ul class="clearfix"> <li <?php if($_GET['cat'] == '') echo 'class="selected"'; ?>><a class="first" href="{:U("Brand/$action_name")}">全部</a></li> <volist name="cat_list" id="vo" key="i"> <li <?php if($_GET['cat'] == $vo['id']) echo 'class="selected"'; ?>><a href="{:U("Brand/$action_name",array('cat'=>$vo['id']))}">{$vo.name}</a></li> </volist> </ul> </div> <script type="text/javascript"> $(function(){ $(document).pjax('.pjax a', '#pjax-container',{ type:'post', scrollTo:false, }); $(document).on('pjax:click', function() { enable_loading = false; }) $(document).on('pjax:send', function(){ var str = "<p class='tc mt-10'>加載中...</p>"; $('#pjax-container').html(str); }) //最后一個(gè)右側(cè)加邊框 $(".brand-news-nav ul li").last().children('a').addClass('last'); $(".brand-news-nav ul li").click(function(){ $(this).addClass('selected').siblings().removeClass('selected'); }) }) </script> <!-- 文章列表頁(yè) --> <div class="wrap clearfix"> <div class="brand-news-list fl" id="pjax-container"> <include file="Brand:article_pjax" /> </div> <div class="brand-news-right fr pb-20"> <a href="{$adv3[0]['url']}"><img class="scrollLoading" data-url="{$adv3[0]['images']|showImagePath}" src="__PUBLIC__/index/images/loading270x160.gif" width="260" height="150"></a> <p class="title mt-10">法律支持</p> <ul class="bgc-fff"> <volist name="law_list" id="vo"> <a href="{:U('law',array('id'=>$vo['id']))}"><li>{$vo.name}</li></a> </volist> </ul> <button class="btn btn-right mt-10 btn-consult">免費(fèi)咨詢</button> <script type="text/javascript"> $(function(){ //最后一個(gè)需要添加一個(gè)last的樣式 $(".brand-news-right li:last").addClass('last'); }) </script> </div> </div> </div>
服務(wù)端代碼
if(is_pjax()){ $this->display('article_pjax'); }else{ $this->display('article'); }
//判斷是否是pjax請(qǐng)求 function is_pjax(){ return array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX']; }
其中的主要思想就是當(dāng).pjax a進(jìn)行點(diǎn)擊的時(shí)候,將#pjax-container的內(nèi)容替換為請(qǐng)求后的內(nèi)容。在后端處理時(shí)需要判斷是否是pjax請(qǐng)求,如果是需要進(jìn)行局部渲染,如果不是進(jìn)行全部渲染。
因?yàn)閜jax用到了HTML5技術(shù),如果瀏覽器不支持Html5那么網(wǎng)站會(huì)正常進(jìn)行跳轉(zhuǎn)式的加載,如果支持那么只是進(jìn)行局部渲染(但是瀏覽器地址欄中的url會(huì)正常跟著a鏈接進(jìn)行變動(dòng))。
注意上述的js代碼中在配置pjax時(shí)有個(gè)參數(shù)scrollTo:false,加上此參數(shù)表示點(diǎn)擊連接后網(wǎng)頁(yè)的scrollBar不會(huì)變動(dòng),如沒(méi)有此參數(shù),每次點(diǎn)擊時(shí)瀏覽視窗會(huì)自動(dòng)跳轉(zhuǎn)到網(wǎng)頁(yè)頂部
小結(jié):Pjax實(shí)際就是從服務(wù)器端返回一段代碼片段,而不用刷新頁(yè)面,并且同時(shí)對(duì) url 地址進(jìn)行修改,這樣可以節(jié)省資源加載,提升頁(yè)面加載速度。
附: pjax的github項(xiàng)目地址 https://github.com/defunkt/jquery-pjax
相關(guān)文章
基于jQuery實(shí)現(xiàn)頂部導(dǎo)航欄功能
這篇文章主要為大家詳細(xì)介紹了基于jQuery實(shí)現(xiàn)頂部導(dǎo)航欄功能,jQuery三級(jí)下拉列表導(dǎo)航菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12jquery+ajax實(shí)現(xiàn)跨域請(qǐng)求的方法
這篇文章主要介紹了jquery+ajax實(shí)現(xiàn)跨域請(qǐng)求的方法,詳細(xì)介紹了前臺(tái)及后臺(tái)的處理方法,是非常實(shí)用的技巧,需要的朋友可以參考下2015-01-01使用jquery獲取url以及jquery獲取url參數(shù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇使用jquery獲取url以及jquery獲取url參數(shù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05jquery實(shí)現(xiàn)吸頂導(dǎo)航效果
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)吸頂導(dǎo)航效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01jQuery實(shí)現(xiàn)為動(dòng)態(tài)添加的元素綁定事件實(shí)例分析
這篇文章主要介紹了jQuery實(shí)現(xiàn)為動(dòng)態(tài)添加的元素綁定事件,結(jié)合實(shí)例形式分析了jQuery常見(jiàn)的事件綁定相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-09-09JQuery select控件的相關(guān)操作實(shí)現(xiàn)代碼
JQuery獲取和設(shè)置Select選項(xiàng)方法匯總?cè)缦拢枰呐笥芽梢詤⒖枷?/div> 2012-09-09最新評(píng)論