静态页
<!DOCTYPE html>
<html>
<head>
<title>Laravel 分页滚动加载</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
.ajax-load{
background: #e1e1e1;
padding: 10px 0px;
width: 100%;
}
</style>
</head>
<body>
<div class="container">
<h2 class="text-center">Laravel 分页滚动加载</h2>
<br/>
<div class="col-md-12" id="post-data">
@foreach($posts as $post)
<div>
<h3><a href="">1111</a></h3>
<p>2222</p>
<div class="text-right">
<button class="btn btn-success">Read More</button>
</div>
<hr style="margin-top:5px;">
</div>
@endforeach
</div>
</div>
<div class="ajax-load text-center" style="display:none">
<p>![](./loader.gif)加载更多……</p>
</div>
<script type="text/javascript">
var page = 1;
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() + 1>= $(document).height()) {
page++;
loadMoreData(page);
}
});
function loadMoreData(page){
$.ajax(
{
url: '?page=' + page,
type: "get",
beforeSend: function()
{
$('.ajax-load').show();
}
})
.done(function(data)
{
//console.log(data.html);
if(data.html == " "){
$('.ajax-load').html("没有数据了……");
return;
}
$('.ajax-load').hide();
$("#post-data").append(data.html);
})
.fail(function(jqXHR, ajaxOptions, thrownError)
{
alert('服务未响应……');
});
}
</script>
</body>
</html>
控制器
public function index(Request $request)
{
$posts = Db::table('banner')->paginate(10);
if ($request->ajax()) {
$view = view('data',compact('posts'))->render();
return response()->json(['html'=>$view]);
}
return view('welcome',compact('posts'));
}