forked from mescroll/mescroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmescroll.min.js
14 lines (14 loc) · 15.7 KB
/
mescroll.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
* mescroll -- 精致的下拉刷新和上拉加载js框架 ( a great JS framework for pull-refresh and pull-up-loading )
* version 1.2.5
* 2017-11-25
* author: wenju < mescroll@qq.com > 文举
*
* 官网: http://www.mescroll.com
* 文档: https://github.com/mescroll/mescroll.git
* 动态: https://github.com/mescroll/mescroll-versions
* 问答:http://www.mescroll.com/qa.html
* issues: https://github.com/mescroll/mescroll/issues
* QQ交流群: 633126761
*/
;(function(a,b){var c=typeof define==="function",d=typeof module!=="undefined"&&module.exports;if(c){define(b)}else{if(d){module.exports=b()}else{this[a]=b()}}})("MeScroll",function(){var a=function(b,e){var h=this;h.isScrollBody=(!b||b=="body");h.scrollDom=h.isScrollBody?document.body:document.getElementById(b);if(!h.scrollDom){console.error('找不到id为"'+b+'"的DOM元素: document.getElementById("'+b+'")==null');return}h.options=e||{};var f=h.options.down&&h.options.down.callback;var d=navigator.userAgent;var c=!!d.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);var i=typeof window.orientation=="undefined";var g=d.indexOf("Android")>-1||d.indexOf("Adr")>-1;h.os={ios:c,pc:i,android:g};if(h.os.ios&&!h.isScrollBody){h.bounceDom=h.scrollDom.getElementsByClassName("mescroll-bounce")[0];if(h.bounceDom){h.isBounceDisable=true;h.bounceDom.style.minHeight=(h.getClientHeight()+5)+"px"}}h.isDownScrolling=false;h.isUpScrolling=false;h.initDownScroll();h.initUpScroll();setTimeout(function(){if(h.optDown.auto){if(h.optDown.autoShowLoading){h.triggerDownScroll()}else{h.optDown.callback&&h.optDown.callback(h)}}if(h.optDown.auto&&h.optUp.auto&&!f){}else{h.optUp.auto&&h.triggerUpScroll()}},30)};a.prototype.extendDownScroll=function(b){a.extend(b,{use:true,auto:true,autoShowLoading:false,isLock:false,isBoth:false,offset:80,outOffsetRate:0.2,bottomOffset:20,minAngle:45,hardwareClass:"mescroll-hardware",warpId:null,warpClass:"mescroll-downwarp",resetClass:"mescroll-downwarp-reset",htmlContent:'<p class="downwarp-progress"></p><p class="downwarp-tip">下拉刷新 </p>',inited:function(d,c){d.downTipDom=c.getElementsByClassName("downwarp-tip")[0];d.downProgressDom=c.getElementsByClassName("downwarp-progress")[0]},inOffset:function(c){if(c.downTipDom){c.downTipDom.innerHTML="下拉刷新"}if(c.downProgressDom){c.downProgressDom.classList.remove("mescroll-rotate")}},outOffset:function(c){if(c.downTipDom){c.downTipDom.innerHTML="释放更新"}},onMoving:function(d,f,c){if(d.downProgressDom){var e=360*f;d.downProgressDom.style.webkitTransform="rotate("+e+"deg)";d.downProgressDom.style.transform="rotate("+e+"deg)"}},beforeLoading:function(d,c){return false},showLoading:function(c){if(c.downTipDom){c.downTipDom.innerHTML="加载中 ..."}if(c.downProgressDom){c.downProgressDom.classList.add("mescroll-rotate")}},callback:function(c){c.resetUpScroll()}})};a.prototype.extendUpScroll=function(b){var c=this.os.pc;a.extend(b,{use:true,auto:false,isLock:false,isBoth:false,callback:null,page:{num:0,size:10,time:null},noMoreSize:5,offset:100,toTop:{src:null,offset:1000,warpClass:"mescroll-totop",showClass:"mescroll-fade-in",hideClass:"mescroll-fade-out",duration:300},loadFull:{use:false,delay:500},empty:{warpId:null,icon:null,tip:"暂无相关数据~",btntext:"",btnClick:null},clearId:null,clearEmptyId:null,hardwareClass:"mescroll-hardware",warpId:null,warpClass:"mescroll-upwarp",htmlLoading:'<p class="upwarp-progress mescroll-rotate"></p><p class="upwarp-tip">加载中..</p>',htmlNodata:'<p class="upwarp-nodata">-- END --</p>',inited:function(d,e){},showLoading:function(d,e){e.innerHTML=d.optUp.htmlLoading},showNoMore:function(d,e){e.innerHTML=d.optUp.htmlNodata},onScroll:null,scrollbar:{use:c,barClass:"mescroll-bar"}})};a.extend=function(c,b){if(!c){return b}for(key in b){if(c[key]==null){c[key]=b[key]}else{if(typeof c[key]=="object"){a.extend(c[key],b[key])}}}return c};a.prototype.initDownScroll=function(){var b=this;b.optDown=b.options.down||{};if(b.optDown.use==false){return}b.extendDownScroll(b.optDown);b.downwarp=document.createElement("div");b.downwarp.className=b.optDown.warpClass;b.downwarp.innerHTML='<div class="downwarp-content">'+b.optDown.htmlContent+"</div>";b.downparent=b.optDown.warpId?document.getElementById(b.optDown.warpId):b.scrollDom;if(b.optDown.warpId){b.downparent.appendChild(b.downwarp)}else{b.downparent.insertBefore(b.downwarp,b.scrollDom.firstChild)}b.touchstartEvent=function(g){if(b.isScrollTo){g.preventDefault()}b.startTop=b.getScrollTop();if(b.isBounceDisable){b.bounceDom.style.minHeight=(b.getClientHeight()+5)+"px";if(b.startTop<=0){b.setScrollTop(1);b.startTop=1}else{if(b.optUp.isLock){var f=b.getScrollHeight();var c=b.getClientHeight();var d=f-c-b.startTop;if(d<=0){b.startTop=f-c-1;b.setScrollTop(b.startTop)}}}}b.endTouchmove=false;b.maxTouchmoveY=b.getBodyHeight()-b.optDown.bottomOffset;if(!b.os.ios){b.startY=g.touches?g.touches[0].pageY:g.clientY}};b.scrollDom.addEventListener("touchstart",b.touchstartEvent);b.scrollDom.addEventListener("mousedown",b.touchstartEvent);b.touchmoveEvent=function(j){if(b.isBounceDisable&&b.isDownScrolling){j.preventDefault();return}var f=b.getScrollTop();if(b.startTop!=null&&f<=0&&!b.isDownScrolling&&(!b.isUpScrolling||(b.isUpScrolling&&b.optUp.isBoth))&&!b.optDown.isLock&&!b.endTouchmove){if(!b.os.ios&&b.startTop>0){return}var d=j.touches?j.touches[0].pageX:j.clientX;var c=j.touches?j.touches[0].pageY:j.clientY;if(!b.preX){b.preX=d}if(!b.preY){b.preY=c}var m=Math.abs(b.preX-d);var l=Math.abs(b.preY-c);var k=Math.sqrt(m*m+l*l);var n=c-b.preY;b.preX=d;b.preY=c;if(k!=0){var h=Math.asin(l/k)/Math.PI*180;if(h<b.optDown.minAngle){return}}if(!b.startY&&b.os.ios){b.startY=c}var g=c-b.startY-f;if(g>0){j.preventDefault();if(b.maxTouchmoveY>0&&c>=b.maxTouchmoveY){b.endTouchmove=true;b.touchendEvent();return}if(!b.downHight){b.downHight=0}if(b.downHight<b.optDown.offset){if(b.movetype!=1){b.movetype=1;b.optDown.inOffset(b);b.downwarp.classList.remove(b.optDown.resetClass);b.scrollDom.classList.add(b.optDown.hardwareClass);b.scrollDom.style.webkitOverflowScrolling="auto";b.isMoveDown=true}b.downHight+=n}else{if(b.movetype!=2){b.movetype=2;b.optDown.outOffset(b);b.downwarp.classList.remove(b.optDown.resetClass);b.scrollDom.classList.add(b.optDown.hardwareClass);b.scrollDom.style.webkitOverflowScrolling="auto";b.isMoveDown=true}if(n>0){b.downHight+=n*b.optDown.outOffsetRate}else{b.downHight+=n}}b.downwarp.style.height=b.downHight+"px";var i=b.downHight/b.optDown.offset;b.optDown.onMoving(b,i,b.downHight)}}};b.scrollDom.addEventListener("touchmove",b.touchmoveEvent);b.scrollDom.addEventListener("mousemove",b.touchmoveEvent);b.touchendEvent=function(){if(b.isMoveDown){if(b.downHight>=b.optDown.offset){b.triggerDownScroll()}else{b.downwarp.classList.add(b.optDown.resetClass);b.downHight=0;b.downwarp.style.height=0}b.scrollDom.style.webkitOverflowScrolling="touch";b.scrollDom.classList.remove(b.optDown.hardwareClass);b.movetype=0;b.isMoveDown=false}b.startY=0;b.preX=0;b.preY=0;b.startTop=null};b.scrollDom.addEventListener("touchend",b.touchendEvent);b.scrollDom.addEventListener("mouseup",b.touchendEvent);b.scrollDom.addEventListener("mouseleave",b.touchendEvent);setTimeout(function(){b.optDown.inited(b,b.downwarp)},0)};a.prototype.triggerDownScroll=function(){if(!this.optDown.beforeLoading(this,this.downwarp)){this.showDownScroll();this.optDown.callback&&this.optDown.callback(this)}};a.prototype.showDownScroll=function(){this.isDownScrolling=true;this.optDown.showLoading(this);this.downHight=this.optDown.offset;this.downwarp.classList.add(this.optDown.resetClass);this.downwarp.style.height=this.optDown.offset+"px"};a.prototype.endDownScroll=function(){this.downHight=0;this.downwarp.style.height=0;this.isDownScrolling=false;if(this.downProgressDom){this.downProgressDom.classList.remove("mescroll-rotate")}};a.prototype.lockDownScroll=function(b){if(b==null){b=true}this.optDown.isLock=b};a.prototype.initUpScroll=function(){var c=this;c.optUp=c.options.up||{use:false};c.extendUpScroll(c.optUp);if(c.optUp.scrollbar.use){c.scrollDom.classList.add(c.optUp.scrollbar.barClass)}if(c.isBounceDisable){c.setBounceDisable(true)}if(c.optUp.use==false){return}c.upwarp=document.createElement("div");c.upwarp.className=c.optUp.warpClass;var b;if(c.optUp.warpId){b=document.getElementById(c.optUp.warpId)}else{if(c.isBounceDisable){if(c.bounceDom){b=c.bounceDom}}}c.upparent=b||c.scrollDom;c.upparent.appendChild(c.upwarp);c.preScrollY=0;c.scrollEvent=function(){var f=c.getScrollTop();var e=f-c.preScrollY>0;c.preScrollY=f;if(!c.isUpScrolling&&(!c.isDownScrolling||(c.isDownScrolling&&c.optDown.isBoth))){if(!c.optUp.isLock){var d=c.getScrollHeight()-c.getClientHeight()-f;if(d<=c.optUp.offset&&e){c.triggerUpScroll()}}if(c.optUp.toTop.src){if(f>=c.optUp.toTop.offset){c.showTopBtn()}else{c.hideTopBtn()}}}c.optUp.onScroll&&c.optUp.onScroll(c,f,e)};if(c.isScrollBody){window.addEventListener("scroll",c.scrollEvent)}else{c.scrollDom.addEventListener("scroll",c.scrollEvent)}setTimeout(function(){c.optUp.inited(c,c.upwarp)},0)};a.prototype.setBounceDisable=function(b){if(this.isScrollBody||!this.os.ios){return}if(b){this.isBounceDisable=true;window.addEventListener("touchmove",this.bounceTouchmove,false)}else{this.isBounceDisable=false;window.removeEventListener("touchmove",this.bounceTouchmove,false)}};a.prototype.bounceTouchmove=function(h){var j=this;var d=h.target;var f=true;while(d!==document.body&&d!==document){var n=d.classList;if(n){if(n.contains("mescroll")||n.contains("mescroll-touch")){f=false;break}else{if(n.contains("mescroll-touch-x")||n.contains("mescroll-touch-y")){var c=h.touches?h.touches[0].pageX:h.clientX;var b=h.touches?h.touches[0].pageY:h.clientY;if(!j.preWinX){j.preWinX=c}if(!j.preWinY){j.preWinY=b}var l=Math.abs(j.preWinX-c);var k=Math.abs(j.preWinY-b);var i=Math.sqrt(l*l+k*k);var m=b-j.preWinY;j.preWinX=c;j.preWinY=b;if(i!=0){var g=Math.asin(k/i)/Math.PI*180;if((g<=45&&n.contains("mescroll-touch-x"))||(g>45&&n.contains("mescroll-touch-y"))){f=false;break}}}}}d=d.parentNode}f&&h.preventDefault()};a.prototype.triggerUpScroll=function(){this.showUpScroll();this.optUp.page.num++;this.optUp.callback&&this.optUp.callback(this.optUp.page,this)};a.prototype.showUpScroll=function(){this.isUpScrolling=true;this.upwarp.classList.add(this.optUp.hardwareClass);this.upwarp.style.visibility="visible";this.optUp.showLoading(this,this.upwarp)};a.prototype.showNoMore=function(){this.upwarp.style.visibility="visible";this.optUp.isLock=true;this.optUp.showNoMore(this,this.upwarp)};a.prototype.hideUpScroll=function(){this.upwarp.style.visibility="hidden";this.upwarp.classList.remove(this.optUp.hardwareClass);var b=this.upwarp.getElementsByClassName("upwarp-progress")[0];if(b){b.classList.remove("mescroll-rotate")}};a.prototype.endUpScroll=function(b){if(b!=null){if(b){this.showNoMore()}else{this.hideUpScroll()}}this.isUpScrolling=false};a.prototype.resetUpScroll=function(c){if(this.optUp&&this.optUp.use){var b=this.optUp.page;this.prePageNum=b.num;this.prePageTime=b.time;b.num=1;b.time=null;if(!this.isDownScrolling&&c!=false){if(c==null){this.removeEmpty();this.clearDataList();this.showUpScroll()}else{this.showDownScroll()}}this.optUp.callback&&this.optUp.callback(b,this)}};a.prototype.setPageNum=function(b){this.optUp.page.num=b-1};a.prototype.setPageSize=function(b){this.optUp.page.size=b};a.prototype.clearDataList=function(){var c=this.optUp.clearId||this.optUp.clearEmptyId;if(c){var b=document.getElementById(c);if(b){b.innerHTML=""}}};a.prototype.endByPage=function(c,e,d){var b;if(this.optUp.use&&e!=null){b=this.optUp.page.num<e}this.endSuccess(c,b,d)};a.prototype.endBySize=function(d,c,e){var b;if(this.optUp.use&&c!=null){var f=(this.optUp.page.num-1)*this.optUp.page.size+d;b=f<c}this.endSuccess(d,b,e)};a.prototype.endSuccess=function(d,b,f){if(this.isDownScrolling){this.endDownScroll()}if(this.optUp.use){var g=this.optUp.page.num;var c=this.optUp.page.size;if(g==1){this.clearDataList()}var e;if(d!=null){if(d<c||b==false){this.optUp.isLock=true;if(d==0&&g==1){e=false;this.showEmpty()}else{var h=(g-1)*c+d;if(h<this.optUp.noMoreSize){e=false}else{e=true}this.removeEmpty()}}else{e=false;this.optUp.isLock=false;this.removeEmpty()}}if(g==1&&f){this.optUp.page.time=f}this.endUpScroll(e);this.loadFull()}};a.prototype.endErr=function(){if(this.isDownScrolling){var b=this.optUp.page;if(b&&this.prePageNum){b.num=this.prePageNum;b.time=this.prePageTime}this.endDownScroll()}if(this.isUpScrolling){this.optUp.page.num--;this.endUpScroll(false)}};a.prototype.loadFull=function(){var b=this;if(b.optUp.loadFull.use&&!b.optUp.isLock&&b.getScrollHeight()<=b.getClientHeight()){setTimeout(function(){if(b.getScrollHeight()<=b.getClientHeight()){b.triggerUpScroll()}},b.optUp.loadFull.delay)}};a.prototype.lockUpScroll=function(b){if(b==null){b=true}this.optUp.isLock=b};a.prototype.showEmpty=function(){var c=this;var d=c.optUp.empty;var b=d.warpId||c.optUp.clearEmptyId;if(b==null){return}var g=document.getElementById(b);if(g){c.removeEmpty();var f="";if(d.icon){f+='<img class="empty-icon" src="'+d.icon+'"/>'}if(d.tip){f+='<p class="empty-tip">'+d.tip+"</p>"}if(d.btntext){f+='<p class="empty-btn">'+d.btntext+"</p>"}c.emptyDom=document.createElement("div");c.emptyDom.className="mescroll-empty";c.emptyDom.innerHTML=f;g.appendChild(c.emptyDom);if(d.btnClick){var e=c.emptyDom.getElementsByClassName("empty-btn")[0];e.onclick=function(){d.btnClick()}}}};a.prototype.removeEmpty=function(){if(this.emptyDom){var b=this.emptyDom.parentNode;if(b){b.removeChild(this.emptyDom)}this.emptyDom=null}};a.prototype.showTopBtn=function(){if(!this.topBtnShow){this.topBtnShow=true;var b=this;var c=b.optUp.toTop;if(b.toTopBtn==null){b.toTopBtn=document.createElement("img");b.toTopBtn.className=c.warpClass;b.toTopBtn.src=c.src;b.toTopBtn.onclick=function(){b.scrollTo(0,b.optUp.toTop.duration)};document.body.appendChild(b.toTopBtn)}b.toTopBtn.classList.remove(c.hideClass);b.toTopBtn.classList.add(c.showClass)}};a.prototype.hideTopBtn=function(){if(this.topBtnShow&&this.toTopBtn){this.topBtnShow=false;this.toTopBtn.classList.remove(this.optUp.toTop.showClass);this.toTopBtn.classList.add(this.optUp.toTop.hideClass)}};a.prototype.scrollTo=function(j,l){if(l==0){this.setScrollTop(j);return}l=l||300;var g=20;var f=l/g;var h=this;var b=h.getScrollHeight()-h.getClientHeight();if(j<0){j=0}if(j>b){j=b}var k=h.getScrollTop()-j;if(k==0){return}var c=k/f;h.isScrollTo=true;var d=0;var e=window.setInterval(function(){if(d<f){if(d==f-1){h.setScrollTop(j)}else{var i=h.getScrollTop()-c;h.setScrollTop(i)}d++}else{h.isScrollTo=false;window.clearInterval(e)}},g)};a.prototype.getScrollHeight=function(){return this.scrollDom.scrollHeight};a.prototype.getClientHeight=function(){if(this.isScrollBody&&document.compatMode=="CSS1Compat"){return document.documentElement.clientHeight}else{return this.scrollDom.clientHeight}};a.prototype.getBodyHeight=function(){return document.body.clientHeight||document.documentElement.clientHeight};a.prototype.getScrollTop=function(){if(this.isScrollBody){return document.documentElement.scrollTop||document.body.scrollTop}else{return this.scrollDom.scrollTop}};a.prototype.setScrollTop=function(b){if(this.isScrollBody){document.documentElement.scrollTop=b;document.body.scrollTop=b}else{this.scrollDom.scrollTop=b}};a.prototype.destroy=function(){var b=this;if(b.optDown.use){b.scrollDom.removeEventListener("touchstart",b.touchstartEvent);b.scrollDom.removeEventListener("mousedown",b.touchstartEvent);b.scrollDom.removeEventListener("touchmove",b.touchmoveEvent);b.scrollDom.removeEventListener("mousemove",b.touchmoveEvent);b.scrollDom.removeEventListener("touchend",b.touchendEvent);b.scrollDom.removeEventListener("mouseup",b.touchendEvent);b.scrollDom.removeEventListener("mouseleave",b.touchendEvent);b.downparent&&b.downparent.removeChild(b.downwarp)}if(b.optUp.use){if(b.isScrollBody){window.removeEventListener("scroll",b.scrollEvent)}else{b.scrollDom.removeEventListener("scroll",b.scrollEvent)}b.upparent&&b.upparent.removeChild(b.upwarp)}b.setBounceDisable(false)};return a});