开发H5项目的时候我们总是需要用到下拉滚动刷新的方式加载页面。这里用Laravel实现一下,直接上代码:
创建模型
这里我们不妨创建一个文章(员额)模型,并且生成测试数据50 条吧。
php artisan make:model -m模型Post.php
命名空间应用使用照明数据库雄辩模型;班级邮报扩展了模型{ public $fillable=['title ',' description '];}迁移文件
使用照亮数据库模式蓝图;使用照明数据库迁移迁移;类CreatePostTable扩展了迁移{ /** *运行迁移* * @ return void */public function up(){ schema : create(' post '),function(蓝图$ table){ $ table-增量(' id ');$ table-string(' title ');$ table-text(' description ');$ table-时间戳();});} /** *反转迁移* * @ return void */public function down(){ schema : drop(' post ');}}测试数据ModelFactory.php
$ factory-define(App post : CLaSS,function(Faker Generator $ Faker){ return[' title '=$ Faker-句子,' description '=$ Faker-段落,];});填充
?phpuse照明数据库播种机;类资料库播种机扩展了播种机{ /** *运行数据库种子* * @ return void */public function run(){//$ this-call(user stableseeder :类);工厂(AppPost:class,50)-创建();}}路由
Route:get('我的帖子','电子邮件保护');控制器
命名空间应用 Http 控制器使用照明 Http 请求使用应用 Http 请求使用应用帖子类后控制器扩展控制器{公共函数我的帖子(Request $ Request){ $ post=post : paginate(6);if($ request-Ajax()){ $ view=view(' data '、compact(' post '))-render();return response()-JSON([' html '=$ view]);}返回视图(“我的帖子”,压缩(“帖子”);}}视图文件参考资料/视图/my-post.php
!DOCTYPE html html标题laravel分页滚动加载/title脚本src=' http :http://libs。百度。com/jquery/2。0 .0/jquery。量滴js '/脚本链接。百度。com/bootstrap/3。0 .3/CSS/bootstrap。量滴CSS“rel=”外部no跟随“rel=”样式表样式类型='text/css ' .Ajax-load {后台: # E1 E1;padding: 10px 0px宽度: 100%;}/style/head dydiv class=' container ' H2 class=' text-center ' Laravel分页滚动加载/H2 br/div class=' col-MD-12 ' id=' post-data ' @ include(' data ')/div/div class=' Ajax-load text-center ' style=' display : none ' p![](./loader.gif)加载更多……/p/div脚本类型=' text/JavaScript ' var page=1;$(窗口)。滚动(函数(){ if($(window)).scrollTop() $(窗口)。高度()1=$(文档)。height()){ page;loadMoreData(第页);} });函数loadMoreData(页面){ $ .ajax({ url: '?page=' page,键入: 'get ',beforeSend:函数(){ $(')。Ajax加载').show();} }) .done(函数(数据)){//console。日志(数据。html);if(data.html==' '){ $(' .Ajax加载').html(")没有数据了……');返回;} $('.Ajax加载').hide();$(' #后数据')。追加(数据。html);}) .fail(function(jqXHR,ajaxOptions,thrownError) { alert('服务未响应……');});}/脚本/正文/html资源/视图/数据。服务器端编程语言(Professional Hypertext Preprocessor的缩写)
@ foreach($ post as $ post)div h3a 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 33605 px;/div@endforeach效果:
以上这篇Laravel简单实现埃阿斯滚动加载示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。