html js,要引入layui.js php Controller控制器 php model模型 ...
html
<div class="layui-container" id="container"> </div>
js,要引入layui.js
layui.use('flow', function() { var $ = layui.jquery; var flow = layui.flow; flow.load({ elem: '#container' //流載入容器 //滾動條所在元素,一般不用填,此處只是演示需要。 ,done: function(page, next){ //執行下一頁的回調 console.log(page) //模擬數據插入 setTimeout(function(){ var lis = []; var url = "/index/index/ajaxNews/?page="+page $.get(url,function (res) { layui.each(res.msg.data, function(index, item) { lis.push('<div class="layui-row list">\ <a href="newsDesc/id/'+item.id+'">\ <div class="layui-col-xs4 layui-col-sm4 ">\ <img src="'+item.cover_img+'">\ </div>\ <div class="layui-col-xs7 layui-col-sm7 right">\ <div class="title">'+item.title+'</div>\ <div class="intro">'+item.intro+'</div>\ </div>\ </a>\ </div>\ <hr/> '); });//組裝html //pages為Ajax返回的總頁數,只有當前頁小於總頁數的情況下,才會繼續出現載入更多 next(lis.join(''), page <= res.msg.pages); }) }, 300); } }); }); </script>
php Controller控制器
public function ajaxNews() { $page = input('page'); //頁碼 $pagesize = 6; $list['data'] = model('Index')->getNewsList($page,$pagesize); $count= model('Index')->getNewsCount(); $list['pages'] = ceil($count/$pagesize); return $list; }
php model模型
// 獲取動態列表 public function getNewsList($page,$pagesize) { $list = Db::name('news') ->field('id,title,intro,cover_img') ->order('create_time desc') ->where(['status'=>0]) ->page($page,$pagesize) ->select(); return $list; } //獲取動態總條數 public function getNewsCount() { $count = Db::name('news')->where(['status'=>0])->count(); return $count; }