- var listData={
- page : 1,
- timers : null,
- pages : 1,
- url : 'http://localhost/test.php',
- dom: '#list_box',
- dom_loadding: '#loadding',
- load:function(page){
- if(!this.last()){
- return;
- }
- let url;
- if(this.url.indexOf("?") != -1){
- url = this.url+'&page='+page
- }else{
- url = this.url+'?page='+page
- }
- let _this = this
- $(_this.dom_loadding).html('加载中。。。')
- $.ajax({
- url: url,
- type: 'post',
- data: '',
- dataType: 'json',
- success: function(res) {
- $(_this.dom).append(res.html);
- _this.page=res.page
- _this.pages=res.pages
- if(_this.page == _this.pages){
- $(_this.dom_loadding).hide()
- }else{
- $(_this.dom_loadding).html('加载更多')
- }
- },
- error: function(xhr, ajaxOptions, thrownError) {
- console.log(thrownError + "---" + xhr.statusText + "---" + xhr.responseText);
- }
- });
- },
- last:function(){
- let dom_loadding = $(this.dom_loadding)
- if(this.page>this.pages){
- if(dom_loadding){
- dom_loadding.html('nothing')
- return false
- }
- return false;
- }
- return true;
- }
- }
- $(function(){
- listData.load(1);
- $(window).scroll(function() {
- if(($(window).height() + $(window).scrollTop() + 60) >= $(document).height()) {
- clearTimeout(listData.timers);
- listData.timers = setTimeout(function() {
- listData.page++;
- listData.load(listData.page);
- }, 300);
- }
- });
- })
复制代码 |