基本思路来源迷津渡口(站长:小指)的文章:http://www.qingzz.cn/emlog_pjax_fixbugs,这里做了一些改动。
何为PJAX?简言之,就是网页点击链接后只改动内容部分而不是加载整个页面,可以减轻服务器压力,减少网络传输,总之,使网页更快的加载。 接下来为网站安装PJAX
1,引入jquery和pjax。
<script src="http://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/jquery.pjax/1.9.6/jquery.pjax.min.js"></script>
2,找到网页的内容部分的容器(如果没有,请自行添加一个div,包含页面中改变的内容部分),以下用id为content的容器举例。
3,在body结束前添加一个script标签,内容为
<script>
$(document).pjax(
'a[target!=_blank]', //a标签中target属性不等于_blank(即不以新窗口打开)的标签全部使用PJAX
'#content', //使用PJAX的容器
{
fragment: '#content',
timeout: 8000 //超时时间为8000毫秒,超过这个时间将刷新
}
);
</script>
4,这个时候PJAX就已经成功安装到网页内了,重新打开网页,按F12调出控制台,点击页面内的链接,可以看到浏览器并没有重新载入整个页面。
我在使用的时候第一次网页并没有像预想的一样,而是重新加载了全部内容,原因是网页原来有一个利用$(document).click事件写的点击出现数字积分的特效,删掉后就可以正常使用了。
5,利用PJAX的时间可以加入"加载中"的动画,具体方法在第三步的script标签中继续添加
$(document).on('pjax:send', function() { //pjax开始
$(".pjax_loading").css("display", "block");
});
$(document).on('pjax:complete', function() { //pjax完成
$(".pjax_loading").css("display", "none");
});
这里提供的是一个小球的动画,你也可以参考原文添加原作者提供的动画:
在body结束前添加加载动画的div
<div class="pjax_loading"> <div class="pjax_spinner"> <span class="ball-loader">Loading…</span> </div> </div>
接着添加一段css:
.pjax_loading { height: 100%; width: 100%; position: fixed; top: 0; left: 0; z-index: 1000; background-color: rgba(90,90,90,.5); display: none; } .pjax_spinner { margin: 300px auto; width: 50px; height: 60px; text-align: center; font-size: 10px; } .pjax_spinner > div { background-color: #67CF22; height: 100%; width: 6px; display: inline-block; -webkit-animation: stretchdelay 1.2s infinite ease-in-out; animation: stretchdelay 1.2s infinite ease-in-out; } @-moz-keyframes ball-loader { 0% { -moz-transform: translate3d(0, 0, 0) scale3d(1, 1, 1); transform: translate3d(0, 0, 0) scale3d(1, 1, 1); -moz-animation-timing-function: ease-in; animation-timing-function: ease-in; } 45% { -moz-transform: translate3d(0, 150px, -10px) scale3d(1, 0.95, 1); transform: translate3d(0, 150px, -10px) scale3d(1, 0.95, 1); -moz-animation-timing-function: ease-in; animation-timing-function: ease-in; } 50% { -moz-transform: translate3d(0, 150px, -10px) scale3d(1, 0.5, 1); transform: translate3d(0, 150px, -10px) scale3d(1, 0.5, 1); -moz-animation-timing-function: linear; animation-timing-function: linear; } 55% { -moz-transform: translate3d(0, 150px, -10px) scale3d(1, 1.25, 1); transform: translate3d(0, 150px, -10px) scale3d(1, 1.25, 1); -moz-animation-timing-function: ease-out; animation-timing-function: ease-out; } } @-webkit-keyframes ball-loader { 0% { -webkit-transform: translate3d(0, 0, 0) scale3d(1, 1, 1); transform: translate3d(0, 0, 0) scale3d(1, 1, 1); -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 45% { -webkit-transform: translate3d(0, 150px, -10px) scale3d(1, 0.95, 1); transform: translate3d(0, 150px, -10px) scale3d(1, 0.95, 1); -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 50% { -webkit-transform: translate3d(0, 150px, -10px) scale3d(1, 0.5, 1); transform: translate3d(0, 150px, -10px) scale3d(1, 0.5, 1); -webkit-animation-timing-function: linear; animation-timing-function: linear; } 55% { -webkit-transform: translate3d(0, 150px, -10px) scale3d(1, 1.25, 1); transform: translate3d(0, 150px, -10px) scale3d(1, 1.25, 1); -webkit-animation-timing-function: ease-out; animation-timing-function: ease-out; } } @keyframes ball-loader { 0% { -moz-transform: translate3d(0, 0, 0) scale3d(1, 1, 1); -ms-transform: translate3d(0, 0, 0) scale3d(1, 1, 1); -webkit-transform: translate3d(0, 0, 0) scale3d(1, 1, 1); transform: translate3d(0, 0, 0) scale3d(1, 1, 1); -moz-animation-timing-function: ease-in; -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 45% { -moz-transform: translate3d(0, 150px, -10px) scale3d(1, 0.95, 1); -ms-transform: translate3d(0, 150px, -10px) scale3d(1, 0.95, 1); -webkit-transform: translate3d(0, 150px, -10px) scale3d(1, 0.95, 1); transform: translate3d(0, 150px, -10px) scale3d(1, 0.95, 1); -moz-animation-timing-function: ease-in; -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 50% { -moz-transform: translate3d(0, 150px, -10px) scale3d(1, 0.5, 1); -ms-transform: translate3d(0, 150px, -10px) scale3d(1, 0.5, 1); -webkit-transform: translate3d(0, 150px, -10px) scale3d(1, 0.5, 1); transform: translate3d(0, 150px, -10px) scale3d(1, 0.5, 1); -moz-animation-timing-function: linear; -webkit-animation-timing-function: linear; animation-timing-function: linear; } 55% { -moz-transform: translate3d(0, 150px, -10px) scale3d(1, 1.25, 1); -ms-transform: translate3d(0, 150px, -10px) scale3d(1, 1.25, 1); -webkit-transform: translate3d(0, 150px, -10px) scale3d(1, 1.25, 1); transform: translate3d(0, 150px, -10px) scale3d(1, 1.25, 1); -moz-animation-timing-function: ease-out; -webkit-animation-timing-function: ease-out; animation-timing-function: ease-out; } } @-moz-keyframes ball-loader-highlight { 0% { -moz-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -moz-animation-timing-function: ease-in; animation-timing-function: ease-in; } 45% { -moz-transform: skew(-30deg, 0) translate3d(0, 0, 1px); transform: skew(-30deg, 0) translate3d(0, 0, 1px); -moz-animation-timing-function: ease-in; animation-timing-function: ease-in; } 50% { -moz-transform: skew(-30deg, 0) translate3d(0, 0, 1px); transform: skew(-30deg, 0) translate3d(0, 0, 1px); -moz-animation-timing-function: linear; animation-timing-function: linear; } 55% { -moz-transform: skew(-30deg, 0) translate3d(0, 0, 1px); transform: skew(-30deg, 0) translate3d(0, 0, 1px); -moz-animation-timing-function: ease-out; animation-timing-function: ease-out; } 100% { -moz-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -moz-animation-timing-function: ease-in; animation-timing-function: ease-in; } } @-webkit-keyframes ball-loader-highlight { 0% { -webkit-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 45% { -webkit-transform: skew(-30deg, 0) translate3d(0, 0, 1px); transform: skew(-30deg, 0) translate3d(0, 0, 1px); -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 50% { -webkit-transform: skew(-30deg, 0) translate3d(0, 0, 1px); transform: skew(-30deg, 0) translate3d(0, 0, 1px); -webkit-animation-timing-function: linear; animation-timing-function: linear; } 55% { -webkit-transform: skew(-30deg, 0) translate3d(0, 0, 1px); transform: skew(-30deg, 0) translate3d(0, 0, 1px); -webkit-animation-timing-function: ease-out; animation-timing-function: ease-out; } 100% { -webkit-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } } @keyframes ball-loader-highlight { 0% { -moz-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -ms-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -webkit-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -moz-animation-timing-function: ease-in; -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 45% { -moz-transform: skew(-30deg, 0) translate3d(0, 0, 1px); -ms-transform: skew(-30deg, 0) translate3d(0, 0, 1px); -webkit-transform: skew(-30deg, 0) translate3d(0, 0, 1px); transform: skew(-30deg, 0) translate3d(0, 0, 1px); -moz-animation-timing-function: ease-in; -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 50% { -moz-transform: skew(-30deg, 0) translate3d(0, 0, 1px); -ms-transform: skew(-30deg, 0) translate3d(0, 0, 1px); -webkit-transform: skew(-30deg, 0) translate3d(0, 0, 1px); transform: skew(-30deg, 0) translate3d(0, 0, 1px); -moz-animation-timing-function: linear; -webkit-animation-timing-function: linear; animation-timing-function: linear; } 55% { -moz-transform: skew(-30deg, 0) translate3d(0, 0, 1px); -ms-transform: skew(-30deg, 0) translate3d(0, 0, 1px); -webkit-transform: skew(-30deg, 0) translate3d(0, 0, 1px); transform: skew(-30deg, 0) translate3d(0, 0, 1px); -moz-animation-timing-function: ease-out; -webkit-animation-timing-function: ease-out; animation-timing-function: ease-out; } 100% { -moz-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -ms-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -webkit-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -moz-animation-timing-function: ease-in; -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } } @-moz-keyframes ball-loader-shadow { 0% { -moz-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -moz-animation-timing-function: ease-in; animation-timing-function: ease-in; } 45% { -moz-transform: translate3d(12.5px, -15px, -1px); transform: translate3d(12.5px, -15px, -1px); -moz-animation-timing-function: ease-in; animation-timing-function: ease-in; } 50% { -moz-transform: translate3d(12.5px, -15px, -1px) scale3d(1, 1, 1); transform: translate3d(12.5px, -15px, -1px) scale3d(1, 1, 1); -moz-animation-timing-function: linear; animation-timing-function: linear; } 55% { -moz-transform: translate3d(12.5px, -15px, -1px); transform: translate3d(12.5px, -15px, -1px); -moz-animation-timing-function: ease-out; animation-timing-function: ease-out; } 100% { -moz-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -moz-animation-timing-function: ease-in; animation-timing-function: ease-in; } } @-webkit-keyframes ball-loader-shadow { 0% { -webkit-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 45% { -webkit-transform: translate3d(12.5px, -15px, -1px); transform: translate3d(12.5px, -15px, -1px); -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 50% { -webkit-transform: translate3d(12.5px, -15px, -1px) scale3d(1, 1, 1); transform: translate3d(12.5px, -15px, -1px) scale3d(1, 1, 1); -webkit-animation-timing-function: linear; animation-timing-function: linear; } 55% { -webkit-transform: translate3d(12.5px, -15px, -1px); transform: translate3d(12.5px, -15px, -1px); -webkit-animation-timing-function: ease-out; animation-timing-function: ease-out; } 100% { -webkit-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } } @keyframes ball-loader-shadow { 0% { -moz-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -ms-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -webkit-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -moz-animation-timing-function: ease-in; -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 45% { -moz-transform: translate3d(12.5px, -15px, -1px); -ms-transform: translate3d(12.5px, -15px, -1px); -webkit-transform: translate3d(12.5px, -15px, -1px); transform: translate3d(12.5px, -15px, -1px); -moz-animation-timing-function: ease-in; -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } 50% { -moz-transform: translate3d(12.5px, -15px, -1px) scale3d(1, 1, 1); -ms-transform: translate3d(12.5px, -15px, -1px) scale3d(1, 1, 1); -webkit-transform: translate3d(12.5px, -15px, -1px) scale3d(1, 1, 1); transform: translate3d(12.5px, -15px, -1px) scale3d(1, 1, 1); -moz-animation-timing-function: linear; -webkit-animation-timing-function: linear; animation-timing-function: linear; } 55% { -moz-transform: translate3d(12.5px, -15px, -1px); -ms-transform: translate3d(12.5px, -15px, -1px); -webkit-transform: translate3d(12.5px, -15px, -1px); transform: translate3d(12.5px, -15px, -1px); -moz-animation-timing-function: ease-out; -webkit-animation-timing-function: ease-out; animation-timing-function: ease-out; } 100% { -moz-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -ms-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -webkit-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -moz-animation-timing-function: ease-in; -webkit-animation-timing-function: ease-in; animation-timing-function: ease-in; } } /* :not(:required) hides this rule from IE9 and below */ .ball-loader:not(:required) { position: relative; display: inline-block; font-size: 0; letter-spacing: -1px; border-radius: 100%; background: #f86; width: 50px; height: 50px; -moz-transform-style: preserve-3d; -webkit-transform-style: preserve-3d; transform-style: preserve-3d; -moz-transform: translate3d(0, 0, 0) scale3d(1, 1, 1); -ms-transform: translate3d(0, 0, 0) scale3d(1, 1, 1); -webkit-transform: translate3d(0, 0, 0) scale3d(1, 1, 1); transform: translate3d(0, 0, 0) scale3d(1, 1, 1); -moz-transform-origin: 0 100%; -ms-transform-origin: 0 100%; -webkit-transform-origin: 0 100%; transform-origin: 0 100%; -moz-animation: ball-loader 1500ms infinite linear; -webkit-animation: ball-loader 1500ms infinite linear; animation: ball-loader 1500ms infinite linear; } .ball-loader:not(:required)::after { content: ''; position: absolute; top: 4.5px; left: 5.5px; width: 15px; height: 15px; background: #ffb099; border-radius: 100%; -moz-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -ms-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -webkit-transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); transform: skew(-20deg, 0) translate3d(0, 2.5px, 1px); -moz-animation: ball-loader-highlight 1500ms infinite linear; -webkit-animation: ball-loader-highlight 1500ms infinite linear; animation: ball-loader-highlight 1500ms infinite linear; } .ball-loader:not(:required)::before { content: ''; position: absolute; top: 50px; left: 5.5px; width: 50px; height: 15px; background: rgba(0, 0, 0, 0.2); border-radius: 100%; -moz-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -ms-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -webkit-transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); transform: translate3d(66.66667px, 66.66667px, -1px) scale3d(1.25, 1.25, 1); -moz-animation: ball-loader-shadow 1500ms infinite linear; -webkit-animation: ball-loader-shadow 1500ms infinite linear; animation: ball-loader-shadow 1500ms infinite linear; -webkit-filter: blur(1px); filter: blur(1px); }
6,关于站长统计,百度统计的兼容方法原文已经写的很详细了,这里不再重复。对于部分script标签失效(例如geetest,一言等等)可以将标签移动到content内部,只要是在content内部的内容,每次都会重载。也可以将执行的js写入一个函数,然后在content的内部调用这个函数,即可解决