Home > Web Front-end > JS Tutorial > Detailed example of the encapsulation process of mui pull-up to load more pull-down refresh data

Detailed example of the encapsulation process of mui pull-up to load more pull-down refresh data

小云云
Release: 2017-12-28 10:25:09
Original
4329 people have browsed it

This article mainly shares with you the encapsulation process of mui pull-up loading to load more pull-down refresh data. Mui pull-up loading and pull-down refresh are similar and both belong to the pullRefresh plug-in. Friends in need can refer to it, I hope it can help everyone.

I used mui to make two projects. When I was free, I pulled up mui to load more, and pulled down to refresh the data to make a simple package. I hope it can help friends in need

demo project The structure

The code is directly pasted

index.html

mui pull-up refresh pull-down load is all here, two methods can be done

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8"> 
 <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
 <title>mui上拉刷新下拉加载demo--封装</title>
 <script src="js/mui.min.js"></script>
 <link href="css/mui.min.css" rel="external nofollow" rel="stylesheet"/>
 <style type="text/css">
 li{
  height: 30px;
 line-height: 30px;
  font-size: 14px;
  color: #bbb;
  text-indent: 4%;
  border-bottom: 1px solid currentColor;
 }
 </style>
</head>
<body>
 <!--下拉刷新容器-->
 <p id="refreshContainer" class="mui-content mui-scroll-wrapper">
  <p class="mui-scroll">
  <!--数据列表-->
  <ul class="mui-table-view mui-table-view-chevron" id="list">
  </ul>
  </p>
 </p>
</body>
<script type="text/javascript" charset="utf-8">
 var pager = {};//分页
 var totalPage;//总页码
 pullRefresh(pager);//启用上拉下拉 
 function pullRefresh(){
  mui("#refreshContainer").pullRefresh({
  up:{
   contentrefresh : "正在加载...",//可选,正在加载状态时,上拉加载控件上显示的标题内容
    contentnomore:'没 有 更 多 数 据 了',//可选,请求完毕若没有更多数据时显示的提醒内容;
   callback:function(){//必选,刷新函数,根据具体业务来编写,比如通过ajax从服务器获取新数据;
     window.setTimeout(function(){
    getData(pager);
    },500);
   }
   },
   down : {
    height:50,//可选,默认50.触发下拉刷新拖动距离,
    auto: true,//可选,默认false.首次加载自动下拉刷新一次
    contentdown : "下拉可以刷新",//可选,在下拉可刷新状态时,下拉刷新控件上显示的标题内容
    contentover : "释放立即刷新",//可选,在释放可刷新状态时,下拉刷新控件上显示的标题内容
    contentrefresh : "正在刷新...",//可选,正在刷新状态时,下拉刷新控件上显示的标题内容
    callback :function(){ //必选,刷新函数,根据具体业务来编写,比如通过ajax从服务器获取新数据;
    window.setTimeout(function(){
     pager['size']= 3;//条数
    pager['page'] = 1;//页码  
    //刷新要先清空父节点里面的子节点
    var f = document.getElementById("list");
    var childs = f.childNodes;
    for(var i = childs.length - 1; i >= 0; i--) {
     f.removeChild(childs[i]);
    }
    getData(pager);
    },500);
    }
   }
  })
 }
 //这个方法是负责向接口请求数据的,你可以选择你喜欢的方式,我这里是直接用的mui.ajax
 function getData(params){
  mui.ajax("/article/getArticlePage",{
   data:{
   "header":{"os":"wap","app":"xxxx","ver":1.0},
   "data":params
   },
   dataType:'json',
   type:'post',
   headers:{'Content-Type':'application/json'},
   success:function(data){
     mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
    //下面这坨都是在拼dom 你可以用jquery 或者是你所熟悉的angular,vue双向绑定
    var returnData = data.data;
    var element=document.getElementById("list");
    var para;
      var node;
    for(var i = 0; i<returnData.length;i++){
    para = document.createElement("li");
    node = document.createTextNode(returnData[i].article_title)
    para.appendChild(node);
    element.appendChild(para);
    }
    //这里很重要,这里获取页码 公式:总条数/每页显示条数
    totalPage = data.ext%pager.size!=0?
    parseInt(data.ext/pager.size)+1:
    data.ext/pager.size;
    if(totalPage==pager.page){//总页码等于当前页码,停止上拉下拉
    mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
    }else{
   pager.page++;
   mui('#refreshContainer').pullRefresh().refresh(true);
    }   
   },
   error:function(xhr,type,errorThrown){
   //异常处理;
   console.log(type);
   }
   })
  } 
</script>
</html>
Copy after login

The code has been posted but it doesn’t look very good

The following is the rendering. I wanted to make a video but found it was too useless to make it. It pissed me off

PS: Let me share with you a sample code: MUI pull-down to load more data

html code

<p id="pullrefresh" class="mui-content mui-scroll-wrapper"> 
    <p id="mui-scroll" class="mui-scroll"> 
      <ul id="refresh-ul" class="mui-table-view"> 
        <!-- 内容 --> 
      </ul> 
    </p> 
  </p> 
<script type="text/javascript"> 
mui('body').on('tap','a',function(){document.location.href=this.href;});//监听让页面A标签的超链接 是有效状态 
mui.init({ 
   pullRefresh : { 
    container:"#pullrefresh",//下拉刷新容器标识,querySelector能定位的css选择器均可,比如:id、.class等 
    up : { 
     height:50,//可选,默认50.触发下拉刷新拖动距离, 
     auto: true,//可选,默认false.自动下拉刷新一次 
     contentdown : "下拉可以刷新",//可选,在下拉可刷新状态时,下拉刷新控件上显示的标题内容 
     contentover : "释放立即刷新",//可选,在释放可刷新状态时,下拉刷新控件上显示的标题内容 
     contentrefresh : "正在刷新...",//可选,正在刷新状态时,下拉刷新控件上显示的标题内容 
     callback :function(){ 
      //业务逻辑代码,比如通过ajax从服务器获取新数据; 
        //注意,加载完新数据后,必须执行如下代码,注意:若为ajax请求,则需将如下代码放置在处理完ajax响应数据之后 
         //mui('#refresh-ul').pullRefresh().endPullupToRefresh(); //refresh completed  
          var pagecount=$("#pagecount").val(); 
          var page=$("#page").val(); 
          pagecount=parseInt(pagecount); 
          page=parseInt(page); 
          page+=1; 
          $.ajax({ 
            url:"/mobile/seckill/order/order_refresh_ajax.jhtml", 
            type: 'post', 
            datatype:'html', 
            data: {'pagecount': pagecount,'page':page}, 
            async: false,//false代表只有在等待ajax执行完毕后才执行 
            success: function(data,msg){ 
              $("#refresh-ul").append(data); 
              $("#page").val(page); 
            }, 
            error: function(data,msg){ 
              alert("error"); 
            } 
            }); 
        this.endPullupToRefresh(false); 
     } //必选,刷新函数,根据具体业务来编写,比如通过ajax从服务器获取新数据; 
    } 
   } 
  }); 
if(mui.os.plus) { 
  mui.plusReady(function() { 
    setTimeout(function() { mui('#pullrefresh').pullRefresh().pullupLoading(); }, 1000); 
  }); 
} else { 
  mui.ready(function() {  
    mui('#pullrefresh').pullRefresh().pullupLoading();  
  }); 
} 
function goShoppingCart() { 
  window.location.href = "/mobile/seckill/shoppingcart.jhtml"; 
} 
</script>
Copy after login

Related recommendations:

PHP functional function packaging with comma added to every three digits

About ajax secondary packaging jquery

JavaScript simulation three ways to achieve encapsulation and the difference introduction

The above is the detailed content of Detailed example of the encapsulation process of mui pull-up to load more pull-down refresh data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template