黑马商城项目_商城分类页_右侧滑动2
来源:互联网 发布:mac 终端删除文件夹 编辑:程序博客网 时间:2024/06/15 06:20
自定义itcast.iScroll
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; } // console.log( that.parentDom); /*如果不存在父容器就停止初始化*/ 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 = {}; //console.log(args.swipeType); /*默认类型 默认的Y轴滑动 如果不是y的话就是以x轴开始滑动*/ that.settings.swipeType = args.swipeType?args.swipeType:'y'; //console.log(that.settings.swipeType); /*默认的缓冲滑动距离*/ 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; } } //console.log("1 " + parentHeight); //console.log("2 " + childHeight); /*缓冲距离*/ var distance = that.settings.swipeDistance; /*区间*/ /*左侧盒子定位的区间*/ that.maxPostion = 0; that.minPostion = -(childHeight-parentHeight); //console.log(" 3 that.minPosition" + that.minPostion); /*设置滑动的当前位置*/ 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.滑动区间*/ /*就是滑动区间*/ //(that.maxPostion+distance)表示刷新显示后,往下拉的状态 //(that.minPostion -distance)表示已经显示到子元素的末尾,再向上拉的状态 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; }};
阅读全文
0 0
- 黑马商城项目_商城分类页_右侧滑动2
- 黑马商城项目_商城分类页_右侧滑动1
- 黑马商城项目_商城分类页_左侧滑动
- 黑马商城项目_布局_主页、分类页、购物车页
- 黑马商城项目_商城主页底部导航
- 黑马商城项目_商城购物车页面
- 黑马商城项目_商城主页_zepto应用
- 黑马商城项目_导航部分
- 黑马商城项目_商品展示结构设计
- 黑马商城项目_制作导航条的圆点
- 黑马商城项目_导航条圆点的定位
- 黑马商城项目_商品展示样式设计
- 尚硅谷Android项目之_硅谷商城项目全套源码解析(四、分类)
- 黑马程序员_学习日记78_806图书商城项目纪要
- 黑马程序员_学习日记79_807图书商城项目纪要
- 黑马程序员_学习日记80_808图书商城项目纪要
- 黑马程序员_学习日记81_810图书商城项目纪要
- 黑马程序员_学习日记82_811图书商城项目纪要
- ngrok使用
- SpringBoot入门
- python中with的用法
- 158. class, static, self, parent
- 第十周项目一
- 黑马商城项目_商城分类页_右侧滑动2
- JVM字节码之整型入栈指令(iconst、bipush、sipush、ldc)
- 第十三周项目1--线性表的顺序查找
- SVN基础
- ubuntu14.04设置静态IP
- GROUP BY 和 ORDER BY 子句联合使用组合查询
- Selenium Python自动化测试学习知识积累-页面常用操作
- 云星数据---Scala实战系列(精品版)】:Scala入门教程004-Scala数组详解003
- HDU 2870(动态规划-最大子矩阵)