ios浏览器调试踩坑(1)----mescroll.js和vue-scroller
程序员文章站
2022-04-10 21:16:52
主要记录在ios浏览器出现触摸无限加载的情况 使用vue-scroller和mescroll.js/mescroll.vue先踩ios浏览器默认滑动会影响mescroll的方法调用。 首先给公共js加入以下代码禁用我们的页面在ios浏览器下会滑动上下页面。 下面给body设置样式: html, bo ......
主要记录在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>
效果
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>
安装教程我就不贴了,自行百度。。。
踩坑之路。莫喷。。。。