thinkPHP實現(xiàn)瀑布流的方法
本文實例講述了thinkPHP實現(xiàn)瀑布流的方法。分享給大家供大家參考。具體分析如下:
很多人都想做瀑布流的效果,這里告訴大家官網(wǎng)使用的方法,首先要下載瀑布流的插件jquery.masonry.min.js 地址:http://masonry.desandro.com/index.html里面包含的很多示例.
流程:
1. 頁面初始化時,調(diào)用插件進(jìn)行一次排版;
2. 當(dāng)用戶將滾動條拖到底部時,用ajax加載一次數(shù)據(jù),并排版顯示
3. 重復(fù)2,直到無數(shù)據(jù)
Html代碼:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!--樣式-->
<style type="text/css">
body {margin:40px auto; width:800px; font-size:12px; color:#666;}
.item{
border: 1px solid #D4D4D4;
color: red;
margin: 0 10px 10px 0;
padding: 10px;
position: relative;
width: 200px;
}
.loading-wrap{
bottom: 50px;
width: 100%;
height: 52px;
text-align: center;
display: none;
}
.loading {
padding: 10px 10px 10px 52px;
height: 32px;
line-height: 28px;
color: #FFF;
font-size: 20px;
border-radius: 5px;
background: 10px center rgba(0,0,0,.7);
}
.footer{
border: 2px solid #D4D4D4;
}
</style>
<!--樣式-->
</head>
<body>
<!--引入所需要的jquery和插件-->
<script type="text/javascript" src="/test/public/Js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/test/public/Js/jquery.masonry.min.js"></script>
<!--引入所需要的jquery和插件-->
<!--瀑布流-->
<div id="container" class=" container">
<!--這里通過設(shè)置每個div不同的高度,來凸顯布局的效果-->
<volist name="height" id="vo">
<div class="item" style="height:{$vo}px;">瀑布流下來了</div>
<input type="hidden" name="last_id" class="last_id" value="{$vo.id}"/>
</volist>
</div>
<!--瀑布流-->
<!--加載中-->
<div id="loading" class="loading-wrap">
<span class="loading">加載中,請稍后...</span>
</div>
<!--加載中-->
<!--尾部-->
<div class="footer"><center>我是頁腳</center></div>
<!--尾部-->
<script type="text/javascript">
//用于轉(zhuǎn)換unix時間戳
function unix_to_datetime(unix)
{
var now = new Date(parseInt(unix) * 1000);
return now.toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
}
$(function(){
//頁面初始化時執(zhí)行瀑布流
var $container = $('#container');
$container.masonry({
itemSelector : '.item',
isAnimated: true
});
//用戶拖動滾動條,達(dá)到底部時ajax加載一次數(shù)據(jù)
var loading = $("#loading").data("on", false);//通過給loading這個div增加屬性on,來判斷執(zhí)行一次ajax請求
$(window).scroll(function(){
if(loading.data("on")) return;
if($(document).scrollTop() > $(document).height()-$(window).height()-$('.footer').height()){//頁面拖到底部了
//加載更多數(shù)據(jù)
loading.data("on", true).fadeIn(); //在這里將on設(shè)為true來阻止繼續(xù)的ajax請求
//獲取最后一個id
var lastid = $('.last_id:last').val();
$.get(
"getMore", //要跳轉(zhuǎn)的頁面
{lastid:lastid},//傳值
function(data){
//獲取到了數(shù)據(jù)data,后面用JS將數(shù)據(jù)新增到頁面上
var getdata = data.data;
var html = "";
if($.isArray(getdata)){
$.each(data.data,function(i,item) {
html += "<div class=\"item\" style=\"height:"+data[i]+"px;\">瀑布又流下來了</div>";
});
var $newElems = $(html).css({ opacity: 0 }).appendTo($container);
$newElems.imagesLoaded(function(){
$newElems.animate({ opacity: 1 });
$container.masonry( 'appended', $newElems, true );
});
//一次請求完成,將on設(shè)為false,可以進(jìn)行下一次的請求
loading.data("on", false);
}
loading.fadeOut();
},
"json"
);
}
});
});
</script>
</body>
</html>
Action代碼:
public function lists(){
$data = D('Info')->order('id DESC')->limit(10)->select();
$this->assign('data', $data);
$this->display();
}
//獲取一次請求的數(shù)據(jù)
public function getMore(){
//獲取最后一個id
if(!emptyempty($_GET['lastid']))$map['id'] = array('lt', $_GET['lastid']);
$data = D('Info')->where($map)->order('id DESC')->limit(10)->select();
$this->ajaxReturn($data);
}
注意:通過判斷窗口是否滾動到頁面底部來決定用ajax加載一次數(shù)據(jù),如果不做處理,會一下子請求很多次,所以,要使用條件來限制.
這里使用的是往一個元素上賦值 $("#loading").data("on", true);,在請求期間判斷是true則不繼續(xù)請求,然后在頁面請求完成后再賦值為false.
希望本文所述對大家的ThinkPHP框架程序設(shè)計有所幫助。
相關(guān)文章
CodeIgniter安全相關(guān)設(shè)置匯總
這篇文章主要介紹了CodeIgniter安全相關(guān)設(shè)置,需要的朋友可以參考下2014-07-07基于Swoole實現(xiàn)PHP與websocket聊天室
本文利用Swoole來實現(xiàn)PHP+websocket的聊天室,過程介紹的很詳細(xì),對聊天室的開發(fā)很有幫助,有需要的可以參考學(xué)習(xí)。2016-08-08laravel手動創(chuàng)建數(shù)組分頁的實現(xiàn)代碼
這篇文章主要介紹了laravel手動創(chuàng)建數(shù)組分頁的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06