forked from mescroll/mescroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mescroll.min.js
11 lines (11 loc) · 13.4 KB
/
mescroll.min.js
1
2
3
4
5
6
7
8
9
10
11
/*
* mescroll -- 精致的下拉刷新和上拉加载js框架 ( a great JS framework for pull-refresh and pull-up-loading )
* version 1.2.3
* 2017-10-17
* author: wenju < [email protected] > 文举
*
* 官网: http://www.mescroll.com
* 文档: https://github.com/mescroll/mescroll.git
* 动态: https://github.com/mescroll/mescroll-versions
*/
;(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(){function a(b,c){this.isScrollBody=(!b||b=="body");this.scrollDom=this.isScrollBody?document.body:document.getElementById(b);this.options=c||{};this.isDownScrolling=false;this.isUpScrolling=false;this.initDownScroll();this.initUpScroll();var d=this;setTimeout(function(){if(d.optDown.auto){if(d.optDown.autoShowLoading){d.triggerDownScroll()}else{d.optDown.callback&&d.optDown.callback(d)}}d.optUp.auto&&d.triggerUpScroll()},30)}a.prototype.extendDownScroll=function(c){var b=!!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);a.extend(c,{use:true,auto:true,autoShowLoading:false,isLock:false,isBoth:false,offset:80,outOffsetRate:0.2,bottomOffset:20,minAngle:45,mustToTop:!b,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(e,d){e.downTipDom=d.getElementsByClassName("downwarp-tip")[0];e.downProgressDom=d.getElementsByClassName("downwarp-progress")[0]},inOffset:function(d){if(d.downTipDom){d.downTipDom.innerHTML="下拉刷新"}if(d.downProgressDom){d.downProgressDom.classList.remove("mescroll-rotate")}},outOffset:function(d){if(d.downTipDom){d.downTipDom.innerHTML="释放更新"}},onMoving:function(e,g,d){if(e.downProgressDom){var f=360*g;e.downProgressDom.style.webkitTransform="rotate("+f+"deg)";e.downProgressDom.style.transform="rotate("+f+"deg)"}},beforeLoading:function(e,d){return false},showLoading:function(d){if(d.downTipDom){d.downTipDom.innerHTML="加载中 ..."}if(d.downProgressDom){d.downProgressDom.classList.add("mescroll-rotate")}},callback:function(d){d.resetUpScroll()}})};a.prototype.extendUpScroll=function(b){var c=typeof window.orientation=="undefined";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(c){if(b.isScrollTo){c.preventDefault()}b.startTop=b.getScrollTop();b.endTouchmove=false;b.maxTouchmoveY=b.getBodyHeight()-b.optDown.bottomOffset;if(b.optDown.mustToTop){b.startY=c.touches?c.touches[0].pageY:c.clientY}};b.scrollDom.addEventListener("touchstart",b.touchstartEvent);b.scrollDom.addEventListener("mousedown",b.touchstartEvent);b.touchmoveEvent=function(j){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.optDown.mustToTop&&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.optDown.mustToTop){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};a.prototype.lockDownScroll=function(b){if(b==null){b=true}this.optDown.isLock=b};a.prototype.initUpScroll=function(){var b=this;b.optUp=b.options.up||{use:false};if(b.optUp.use==false){return}b.extendUpScroll(b.optUp);if(b.optUp.scrollbar.use){b.scrollDom.classList.add(b.optUp.scrollbar.barClass)}b.upwarp=document.createElement("div");b.upwarp.className=b.optUp.warpClass;b.upparent=b.optUp.warpId?document.getElementById(b.optUp.warpId):b.scrollDom;b.upparent.appendChild(b.upwarp);b.preScrollY=0;b.scrollEvent=function(){var e=b.getScrollTop();var d=e-b.preScrollY>0;b.preScrollY=e;if(!b.isUpScrolling&&(!b.isDownScrolling||(b.isDownScrolling&&b.optDown.isBoth))){if(!b.optUp.isLock){var c=b.getScrollHeight()-b.getClientHeight()-e;if(c<=b.optUp.offset&&d){b.triggerUpScroll()}}if(b.optUp.toTop.src){if(e>=b.optUp.toTop.offset){b.showTopBtn()}else{b.hideTopBtn()}}}b.optUp.onScroll&&b.optUp.onScroll(b,e,d)};if(b.isScrollBody){window.addEventListener("scroll",b.scrollEvent)}else{b.scrollDom.addEventListener("scroll",b.scrollEvent)}setTimeout(function(){b.optUp.inited(b,b.upwarp)},0)};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)};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.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)}};return a});