Laravel 簡單實現(xiàn)Ajax滾動加載示例
開發(fā)H5項目的時候我們總是需要用到下拉滾動刷新的方式加載頁面。這里用 Laravel 實現(xiàn)一下,直接上代碼:
創(chuàng)建模型
這里我們不妨創(chuàng)建一個 文章(Post)模型, 并且生成測試數(shù)據(jù) 50 條吧。
php artisan make:model -m
模型Post.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public $fillable = ['title','description'];
}
遷移文件
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop("posts");
}
}
測試數(shù)據(jù) ModelFactory.php
$factory->define(App\Post::class, function (Faker\Generator $faker) {
return [
'title' => $faker->sentence,
'description' => $faker->paragraph,
];
});
填充
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
factory(App\Post::class, 50)->create();
}
}
路由
Route::get('my-post', 'PostController@myPost');
控制器
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
class PostController extends Controller
{
public function myPost(Request $request)
{
$posts = Post::paginate(6);
if ($request->ajax()) {
$view = view('data',compact('posts'))->render();
return response()->json(['html'=>$view]);
}
return view('my-post',compact('posts'));
}
}
視圖文件 resources/view/my-post.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 分頁滾動加載</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<link rel="external nofollow" 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">
@include('data')
</div>
</div>
<div class="ajax-load text-center" style="display:none">
<p>加載更多……</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("沒有數(shù)據(jù)了……");
return;
}
$('.ajax-load').hide();
$("#post-data").append(data.html);
})
.fail(function(jqXHR, ajaxOptions, thrownError)
{
alert('服務(wù)未響應(yīng)……');
});
}
</script>
</body>
</html>
resources/view/data.php
@foreach($posts as $post)
<div>
<h3><a href="">{{ $post->title }}</a></h3>
<p>{{ str_limit($post->description, 400) }}</p>
<div class="text-right">
<button class="btn btn-success">Read More</button>
</div>
<hr style="margin-top:5px;">
</div>
@endforeach
效果:

以上這篇Laravel 簡單實現(xiàn)Ajax滾動加載示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
php getcwd與dirname(__FILE__)區(qū)別詳解
這篇文章主要介紹了php getcwd與dirname(__FILE__)區(qū)別詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09
PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫示例【基于thinkPHP5.1框架】
這篇文章主要介紹了PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫,結(jié)合實例形式分析了基于thinkPHP5.1框架使用ODBC連接SQL Server2008數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
php使用Session和文件統(tǒng)計在線人數(shù)
這篇文章主要介紹了php使用Session和文件統(tǒng)計在線人數(shù),本文直接給出實例代碼,需要的朋友可以參考下2015-07-07
PHP程序中使用adodb連接不同數(shù)據(jù)庫的代碼實例
這篇文章主要介紹了PHP程序中使用adodb連接不同數(shù)據(jù)庫的代碼實例,具體的用法示例中用switch語句寫了一個匯總式的支持,需要的朋友可以參考下2015-12-12
php smarty truncate UTF8亂碼問題解決辦法
這篇文章主要介紹了php smarty truncate UTF8亂碼問題解決辦法,需要的朋友可以參考下2014-06-06
Yii中srbac權(quán)限擴(kuò)展模塊工作原理與用法分析
這篇文章主要介紹了Yii中srbac權(quán)限擴(kuò)展模塊工作原理與用法,結(jié)合實例形式分析了srbac模塊的原理及權(quán)限操作的相關(guān)技巧,需要的朋友可以參考下2016-07-07
Laravel中獲取路由參數(shù)Route Parameters的五種方法示例
這篇文章主要給大家介紹了關(guān)于Laravel中獲取路由參數(shù)Route Parameters的五種方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Laravel具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-09-09

