主要记录在ios浏览器出现触摸无限加载的情况
使用vue-scroller和mescroll.js/mescroll.vue先踩ios浏览器默认滑动会影响mescroll的方法调用。
首先给公共js加入以下代码禁用我们的页面在ios浏览器下会滑动上下页面。
<script> //禁止ios手机双击放大以及缩小 window.onload = function () { // 阻止双击放大 var lastTouchEnd = 0; document.addEventListener('touchstart', function (event) { if (event.touches.length > 1) { event.preventDefault(); } }); document.addEventListener('touchend', function (event) { var now = (new Date()).getTime(); if (now - lastTouchEnd <= 300) { event.preventDefault(); } lastTouchEnd = now; }, false); // 阻止双指放大 document.addEventListener('gesturestart', function (event) { event.preventDefault(); }); //在ios浏览器调试的时候禁止页面滚动 var ios = navigator.userAgent.indexOf('iPhone');//判断是否为ios if (ios != -1) { //ios下运行 var scroll = document.getElementById("scroll")//你需要滑动的dom元素 touchScroll(scroll); } var canScroll = true; function touchScroll(el) { canScroll = false; //el需要滑动的元素 el.addEventListener('touchmove', function (e) { canScroll = true; }) document.body.addEventListener('touchmove', function (e) { // alert(canScroll); if (!canScroll) { e.preventDefault(); //阻止默认事件(上下滑动) } else { //需要滑动的区域 var top = el.scrollTop; //对象最顶端和窗口最顶端之间的距离 var scrollH = el.scrollHeight; //含滚动内容的元素大小 var offsetH = el.offsetHeight; //网页可见区域高 var cScroll = top + offsetH; //当前滚动的距离 //被滑动到最上方和最下方的时候 if (top == 0) { top = 1; //0~1之间的小数会被当成0 } else if (cScroll === scrollH) { el.scrollTop = top - 0.1; } } }, { passive: false }) //passive防止阻止默认事件不生效 } } </script>
下面给body设置样式:
html,
body {
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
}
接下来就是vue页面使用vue-scroller或者mescroll.js/mescroll.vue;
mescroll.js
<template> <div class="bg-box"> <div id="mescroll" class="mescroll"> <div> <mescroll-vue ref="mescroll" :down="mescrollDown" :up="mescrollUp" @init="mescrollInit" class="scrollView"> <div class="invoiceList"> <div v-for="(item,index) in invoiceList" class="invoiceList-box" :key="index"> <a :class="item.isShow?'items-selected':'items'" @click="onItemClick(item,index)"></a> <div class="information"> <p class="numvber">单号:{{item.OrderNo}}</p> <p><span class="icon1"></span>始发地 ———— 目的地</p> <p><span class="icon2"></span>件数 : {{item.Pse}}件</p> <p><span class="icon2"></span>重量 : {{item.Weight}}KG</p> <p class="invoicedate">{{item.DownOrderTime}}</p> </div> <p class="price">¥{{item.TotalMoney}}</p> </div> </div> </mescroll-vue> </div> </div> </div> </template> <script> import MescrollVue from "mescroll.js/mescroll.vue"; export default { components: { MescrollVue }, data() { return { mescroll: null, // mescroll实例对象 mescrollDown: {}, //下拉刷新的配置. (如果下拉刷新和上拉加载处理的逻辑是一样的,则mescrollDown可不用写了) mescrollUp: { // 上拉加载的配置. callback: this.upCallback, // 上拉回调,此处简写; 相当于 callback: function(page, mescroll) { } //以下是一些常用的配置,当然不写也可以的. page: { num: 0, //当前页 默认0,回调之前会加1; 即callback(page)会从1开始 size: 5 //每页数据条数,默认10 }, noMoreSize: 5, //如果列表已无数据,可设置列表的总数量要大于5才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看 toTop: { //回到顶部按钮 src: "./static/mescroll/mescroll-totop.png", //图片路径,默认null,支持网络图 offset: 1000 //列表滚动1000px才显示回到顶部按钮 }, htmlContent: '<p class="downwarp-progress"></p><p class="downwarp-tip">下拉刷新 </p>', //布局内容 empty: { //列表第一页无任何数据时,显示的空提示布局; 需配置warpId才显示 warpId: "xxid", //父布局的id (1.3.5版本支持传入dom元素) icon: "./static/mescroll/mescroll-empty.png", //图标,默认null,支持网络图 tip: "暂无相关数据~" //提示 } } }; }, beforeRouteEnter(to, from, next) { // 如果没有配置回到顶部按钮或isBounce,则beforeRouteEnter不用写 next(vm => { vm.$refs.mescroll.beforeRouteEnter(); // 进入路由时,滚动到原来的列表位置,恢复回到顶部按钮和isBounce的配置 }); }, beforeRouteLeave(to, from, next) { // 如果没有配置回到顶部按钮或isBounce,则beforeRouteLeave不用写 this.$refs.mescroll.beforeRouteLeave(); // 退出路由时,记录列表滚动的位置,隐藏回到顶部按钮和isBounce的配置 next(); }, methods: { mescrollInit(mescroll) { this.mescroll = mescroll; }, upCallback(page, mescroll) { let data = {}; data.uId = 1; data.type = 10; data.pageIndex = page.num; data.pageSize = page.size; let str = `uId=${data.uId}&type=${data.type}&pageIndex=${ data.pageIndex }&pageSize=${data.pageSize}`; this.$http.get(str) //地址换成自己的 .then(res => { if (res && res.Total > 0) { if (page.num == 1) { this.invoiceList = []; } this.invoiceList = this.invoiceList.concat(res.DataList); this.invoiceList.map(item => { this.$set(item, "isShow", false); }); this.$nextTick(() => { mescroll.endSuccess(res.DataList.length); }); } }) .catch(e => { mescroll.endErr(); }); } } }; </script> <style lang="less" scoped> .mescroll { position: absolute; width: 100%; height: 100%; top: 0; bottom: 0; left: 0; right: 0; background: #eee; overflow: auto; -webkit-overflow-scrolling: touch; .mescroll { position: fixed; top: 0; bottom: 0; height: auto; } } </style>
效果
![](https://i-blog.csdnimg.cn/blog_migrate/29fc39d5d427e285a00866264cea9ada.png)
vue-scroller(在main.js引入就不需要再引入了)
附代码:
<template> <div> <div class="content"> <div class="content-box"> <scroller :on-refresh="refresh" :on-infinite="infinite" ref="scrollerBottom" refresh-layer-color="#4b8bf4" loading-layer-color="#ec4949"> <!-- 上拉刷新动画 --> <svg class="spinner" style="stroke: #4b8bf4;" slot="refresh-spinner" viewBox="0 0 64 64"> <g stroke-width="7" stroke-linecap="round"> <line x1="10" x2="10" y1="27.3836" y2="36.4931"> <animate attributeName="y1" dur="750ms" values="16;18;28;18;16;16" repeatCount="indefinite"></animate> <animate attributeName="y2" dur="750ms" values="48;46;36;44;48;48" repeatCount="indefinite"></animate> <animate attributeName="stroke-opacity" dur="750ms" values="1;.4;.5;.8;1;1" repeatCount="indefinite"></animate> </line> <line x1="24" x2="24" y1="18.6164" y2="45.3836"> <animate attributeName="y1" dur="750ms" values="16;16;18;28;18;16" repeatCount="indefinite"></animate> <animate attributeName="y2" dur="750ms" values="48;48;46;36;44;48" repeatCount="indefinite"></animate> <animate attributeName="stroke-opacity" dur="750ms" values="1;1;.4;.5;.8;1" repeatCount="indefinite"></animate> </line> <line x1="38" x2="38" y1="16.1233" y2="47.8767"> <animate attributeName="y1" dur="750ms" values="18;16;16;18;28;18" repeatCount="indefinite"></animate> <animate attributeName="y2" dur="750ms" values="44;48;48;46;36;44" repeatCount="indefinite"></animate> <animate attributeName="stroke-opacity" dur="750ms" values=".8;1;1;.4;.5;.8" repeatCount="indefinite"></animate> </line> <line x1="52" x2="52" y1="16" y2="48"> <animate attributeName="y1" dur="750ms" values="28;18;16;16;18;28" repeatCount="indefinite"></animate> <animate attributeName="y2" dur="750ms" values="36;44;48;48;46;36" repeatCount="indefinite"></animate> <animate attributeName="stroke-opacity" dur="750ms" values=".5;.8;1;1;.4;.5" repeatCount="indefinite"></animate> </line> </g> </svg> <div class="invoiceList"> <div v-for="(item,index) in invoiceList" class="invoiceList-box" @click="InvoiceDetails(item)" :key="index"> <div class="slecets"> </div> <div class="information"> <p class="numvber">单号:{{item.OrderNo}}</p> <p><span class="icon1"></span>始发地 ———— 目的地</p> <p><span class="icon2"></span>件数 : {{item.Pse}}件</p> <p><span class="icon2"></span>重量 : {{item.Weight}}KG</p> <p class="invoicedate">{{item.DownOrderTime}}</p> </div> <p class="bitch">已开发票</p> <p class="price">¥{{item.TotalMoney}}</p> </div> </div> <!-- 下拉加载动画 --> <svg class="spinner" style="fill: #ec4949;" slot="infinite-spinner" viewBox="0 0 64 64"> <g> <circle cx="16" cy="32" stroke-width="0" r="3"> <animate attributeName="fill-opacity" dur="750ms" values=".5;.6;.8;1;.8;.6;.5;.5" repeatCount="indefinite"></animate> <animate attributeName="r" dur="750ms" values="3;3;4;5;6;5;4;3" repeatCount="indefinite"></animate> </circle> <circle cx="32" cy="32" stroke-width="0" r="3.09351"> <animate attributeName="fill-opacity" dur="750ms" values=".5;.5;.6;.8;1;.8;.6;.5" repeatCount="indefinite"></animate> <animate attributeName="r" dur="750ms" values="4;3;3;4;5;6;5;4" repeatCount="indefinite"></animate> </circle> <circle cx="48" cy="32" stroke-width="0" r="4.09351"> <animate attributeName="fill-opacity" dur="750ms" values=".6;.5;.5;.6;.8;1;.8;.6" repeatCount="indefinite"></animate> <animate attributeName="r" dur="750ms" values="5;4;3;3;4;5;6;5" repeatCount="indefinite"></animate> </circle> </g> </svg> </scroller> </div> </div> </div> </template> <script> export default { data() { return { invoiceList: [], pageIndex: 0, pageSize: 5, noDate: false }; }, mounted() {}, methods: { //获取开票历史列表 InvoiceList(offset, fn) { let data = {}; data.uId = 1; data.type = 20; data.pageIndex = offset; data.pageSize = this.pageSize; let str = `uId=${data.uId}&type=${data.type}&pageIndex=${ data.pageIndex }&pageSize=${data.pageSize}`; this.$http.get(str).then(res => { //一样换成自己的接口 if (res && res.Total > 0) { let maxNum = Math.ceil(res.Total / data.pageSize); //判断总共有多少页 if (offset > maxNum) { //如果当前页大于总页数判断显示没有更多数据 fn(true); return; } else { if (fn) { fn(); } } if (this.pageIndex == 1) { this.invoiceList = res.DataList; } else { this.invoiceList = this.invoiceList.concat(res.DataList); } } }); }, //下拉刷新 refresh(done) { this.pageIndex = 1; setTimeout(() => { this.InvoiceList(1, done); }, 1500); }, //上拉加载数据 infinite(done) { setTimeout(() => { this.pageIndex++; this.InvoiceList(this.pageIndex, done); }, 1500); } } }; </script> <style lang="less" scoped> @import "../../main.less"; .content { text-align: center; height: 100%; overflow: hidden; .content-box { position: absolute; width: 100%; top: 3.14rem; bottom: 0; left: 0; right: 0; background: #eee; } } </style>
![](https://i-blog.csdnimg.cn/blog_migrate/c423f90864c19410f89ef379a967a601.png)
安装教程我就不贴了,自行百度。。。
踩坑之路。莫喷。。。。