jq 上拉加载更多

[复制链接]
admin 发表于 2021-12-10 13:47:23 | 显示全部楼层 |阅读模式
  1. var listData={
  2.                         page : 1,
  3.                         timers : null,
  4.                         pages : 1,
  5.                         url : 'http://localhost/test.php',
  6.                         dom: '#list_box',
  7.                         dom_loadding: '#loadding',
  8.                         load:function(page){
  9.                                 if(!this.last()){
  10.                                         return;
  11.                                 }
  12.                                 let url;
  13.                                 if(this.url.indexOf("?") != -1){
  14.                                         url = this.url+'&page='+page
  15.                                 }else{
  16.                                         url = this.url+'?page='+page
  17.                                 }
  18.                                 let _this = this
  19.                                 $(_this.dom_loadding).html('加载中。。。')
  20.                                 $.ajax({
  21.                                         url: url,
  22.                                         type: 'post',
  23.                                         data: '',
  24.                                         dataType: 'json',
  25.                                         success: function(res) {
  26.                                                 $(_this.dom).append(res.html);
  27.                                                 _this.page=res.page
  28.                                                 _this.pages=res.pages
  29.                                                 if(_this.page == _this.pages){
  30.                                                         $(_this.dom_loadding).hide()
  31.                                                 }else{
  32.                                                         $(_this.dom_loadding).html('加载更多')
  33.                                                 }
  34.                                         },
  35.                                         error: function(xhr, ajaxOptions, thrownError) {
  36.                                                 console.log(thrownError + "---" + xhr.statusText + "---" + xhr.responseText);
  37.                                         }
  38.                                 });
  39.                         },
  40.                         last:function(){
  41.                                 let dom_loadding = $(this.dom_loadding)
  42.                                 if(this.page>this.pages){
  43.                                         if(dom_loadding){
  44.                                                 dom_loadding.html('nothing')
  45.                                                 return false
  46.                                         }
  47.                                         return false;
  48.                                 }
  49.                                 return true;
  50.                         }
  51.                 }
  52.                 $(function(){
  53.                         listData.load(1);
  54.                         $(window).scroll(function() {
  55.                                 if(($(window).height() + $(window).scrollTop() + 60) >= $(document).height()) {
  56.                                         clearTimeout(listData.timers);
  57.                                         listData.timers = setTimeout(function() {
  58.                                                 listData.page++;
  59.                                                 listData.load(listData.page);
  60.                                         }, 300);
  61.                                 }
  62.                         });
  63.                 })
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

公告上一条 /1 下一条