主要記錄在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>
安裝教程我就不貼了,自行百度。。。 踩坑之路。莫噴。。。。