/** * created by zhousg on 2016/3/18. */ /** * @author zhousg * @date 2016-02-04 * @method 滑动方法 针对一个大容器内部的容器做滑动封装 * @param args args.swipedom 大容器对象 args.swipetype 滑动类型 args.swipedistance 缓冲距离 * 注意:子容器的高宽度是取的内容的高宽 所以padding的大小有影响 */ if(!window.itcast){ window.itcast = {}; } itcast.iscroll = function(args){ /*调用的时候没有初始化的话就是初始化一次*/ if(!(this instanceof arguments.callee)) return new arguments.callee(args); this.init(args); }; itcast.iscroll.prototype = { constructor:itcast.iscroll, init:function(args){ /*局部变量来接受当前的this*/ var that = this; /*如果传入的对象是一个dom对象就把他看作是我们的大容器盒子*/ if(args.swipedom && typeof args.swipedom == 'object'){ that.parentdom = args.swipedom; } /*如果不存在父容器就停止初始化*/ if(!that.parentdom) return false; /*找到子容器*/ that.childdom = that.parentdom.children&&that.parentdom.children[0]?that.parentdom.children[0]:''; /*如果不存在子容器就停止初始化*/ if(!that.childdom) return false; /*初始化传入的参数*/ that.settings = {}; /*默认类型 默认的y轴滑动 如果不是y的话就是以x轴开始滑动*/ that.settings.swipetype = args.swipetype?args.swipetype:'y'; /*默认的缓冲滑动距离*/ that.settings.swipedistance = args.swipedistance>=0?args.swipedistance:150; /*初始化滑动*/ that._scroll(); }, /*对外开放的设置定位的方法*/ settranslate:function(translate){ this.currpostion = translate; this._addtransition(); this._changetranslate(this.currpostion); }, _addtransition:function(){ this.childdom.style.transition = "all .2s ease"; this.childdom.style.webkittransition = "all .2s ease";/*兼容 老版本webkit内核浏览器*/ }, _removetransition:function(){ this.childdom.style.transition = "none"; this.childdom.style.webkittransition = "none";/*兼容 老版本webkit内核浏览器*/ }, _changetranslate:function(translate){ if(this.settings.swipetype == 'y'){ this.childdom.style.transform = "translatey("+translate+"px)"; this.childdom.style.webkittransform = "translatey("+translate+"px)"; }else{ this.childdom.style.transform = "translatex("+translate+"px)"; this.childdom.style.webkittransform = "translatex("+translate+"px)"; } }, _scroll:function(){ /*局部变量来接受当前的this*/ var that = this; /*滑动的类型*/ var type = that.settings.swipetype == 'y'?true:false; /*父容器的高度或宽度*/ var parentheight = type?that.parentdom.offsetheight:that.parentdom.offsetwidth; /*子容器的高度或宽度*/ var childheight = type?that.childdom.offsetheight:that.childdom.offsetwidth; /*子容器没有父容器大的时候*/ if(childheight < parentheight){ if(type){ that.childdom.style.height = parentheight + 'px'; childheight = parentheight; }else{ that.childdom.style.width = parentheight + 'px'; childheight = parentheight; } } /*缓冲距离*/ var distance = that.settings.swipedistance; /*区间*/ /*左侧盒子定位的区间*/ that.maxpostion = 0; that.minpostion = -(childheight-parentheight); /*设置滑动的当前位置*/ that.currpostion = 0; that.startpostion = 0; that.endpostion = 0; that.movepostion = 0; /*1.滑动*/ that.childdom.addeventlistener('touchstart',function(e){ /*初始的y的坐标*/ that.startpostion = type?e.touches[0].clienty: e.touches[0].clientx; },false); that.childdom.addeventlistener('touchmove',function(e){ e.preventdefault(); /*不停的做滑动的时候记录的endy的值*/ that.endpostion = type?e.touches[0].clienty: e.touches[0].clientx; that.movepostion = that.startpostion - that.endpostion;/*计算了移动的距离*/ /*2.滑动区间*/ /*就是滑动区间*/ if((that.currpostion-that.movepostion)<(that.maxpostion+distance) && (that.currpostion-that.movepostion)>(that.minpostion -distance)){ that._removetransition(); that._changetranslate(that.currpostion-that.movepostion); } },false); window.addeventlistener('touchend',function(e){ /*在限制滑动区间之后 重新计算当前定位*/ /*判断是否在我们的合理定位区间内*/ /*先向下滑动 */ if((that.currpostion-that.movepostion) > that.maxpostion){ that.currpostion = that.maxpostion; that._addtransition(); that._changetranslate(that.currpostion); } /*想上滑动的时候*/ else if((that.currpostion-that.movepostion) < that.minpostion){ that.currpostion = that.minpostion; that._addtransition(); that._changetranslate(that.currpostion); } /*正常的情况*/ else{ that.currpostion = that.currpostion-that.movepostion; } that._reset(); },false); }, _reset:function(){ var that = this; that.startpostion = 0; that.endpostion = 0; that.movepostion = 0; } };