HTML5技术

H5+CSS3实现手指滑动切换图片

字号+ 作者:H5之家 来源:H5之家 2015-10-10 11:00 我要评论( )

H5+CSS3实现手指滑动切换图片 包含3个文件:html、slider-H5.js、jquery.js(自行下载)。在html中可配置滑动参数。具体代码如下: HTML代码: H5手指滑动切换图片 ul,li @media screen and (min-width:240px) { html, body{font-size }@media screen and (min

H5+CSS3实现手指滑动切换图片

包含3个文件:html、slider-H5.js、jquery.js(自行下载)。在html中可配置滑动参数。具体代码如下:

HTML代码:

H5手指滑动切换图片 ul,li @media screen and (min-width:240px) { html, body{ font-size } @media screen and (min-width:320px) { html, body{ font-size } @media screen and (min-width:480px) { html, body{ font-size } @media screen and (min-width:640px) { html, body{ font-size } @media screen and (min-width:960px) { html, body{ font-size } div.imgbox div.imgbox ul div.imgbox ul li div.imgbox ul li img #page这里通过回调显示当前滚动到多少页:0 (function(){ /* 注意:$.mggScrollImg返回的scrollImg对象上有 next,prev,go三个方法,可以实现外部对滚动索引的控制。 如:scrollImg.next();//会切换到下一张图片 scrollImg.go(0);//会切换到第一张图片 ,{ loop : ); } }); })()

View Code

slider-H5.js 代码

(function($){ /* 图片滚动效果 @jQuery or @String box : 滚动列表jQuery对象或者选择器 如:滚动元素为li的外层ul @object config : { @Number width : 一次滚动宽度,默认为box里面第一个一级子元素宽度[如果子元素宽度不均匀则滚动效果会错乱] @Number size : 列表长度,默认为box里面所有一级子元素个数[如果size不等于一级子元素个数,则不支持循环滚动] @Boolean loop : 是否支持循环滚动 默认 true @Boolean auto : 是否自动滚动,支持自动滚动时必须支持循环滚动,否则设置无效,默认为true @Number auto_wait_time : 自动轮播一次时间间隔,默认为:3000ms @Function callback : 滚动完回调函数,参入一个参数当前滚动节点索引值 } */ function mggScrollImg(box,config){ this.box = $(box); this.config = $.extend({},config||{}); .size = this.config.size||this.box.children().length; .auto =.auto_wait_time = .scroll_time =.minleft = -.maxleft =0;.now_left = 0;.point_x = .point_y = .move_left = .index = 0; this.busy = false; this.timer; this.init(); } $.extend(mggScrollImg.prototype,{ init : function(){ this.bind_event(); this.init_loop(); this.auto_scroll(); }, bind_event : function(){ var self = this; self.box.bind('touchstart',function(e){ var t=e.touches?e.touches:e.originalEvent.targetTouches; if(t.length==1 && !self.busy){ self.point_x = t[0].screenX; self.point_y = t[0].screenY; } }).bind('touchmove',function(e){ var t=e.touches?e.touches:e.originalEvent.targetTouches; if(t.length==1 && !self.busy){ return self.move(t[0].screenX,t[0].screenY);//这里根据返回值觉得是否阻止默认touch事件 } }).bind('touchend',function(e){ !self.busy && self.move_end(); }); }, /* 初始化循环滚动,当一次性需要滚动多个子元素时,暂不支持循环滚动效果, 如果想实现一次性滚动多个子元素效果,可以通过页面结构实现 循环滚动思路:复制首尾节点到尾首 */ init_loop : function(){ .now_left = -.minleft = -.maxleft = -this.width; this.box.prepend(this.box.children().eq(this.size-1).clone()).append(this.box.children().eq(1).clone()).css(this.get_style(2)); this.box.css('width',this.width*(this.size+2)); }else{ this.loop = false; this.box.css('width',this.width*this.size); } }, auto_scroll : self = this; if(!self.auto)return; clearTimeout(self.timer); self.timer = setTimeout(function(){ self.go_index(self.index+1); },self.auto_wait_time); }, go_index : self = this; if(self.busy)return; clearTimeout(self.timer); self.busy = true; if(self.loop){//如果循环 ind = ind<0?-1:ind; ind = ind>self.size?self.size:ind; }else{ ind = ind<0?0:ind; ind = ind>=self.size?(self.size-1):ind; } if(!self.loop && (self.now_left == -(self.width*ind))){ self.complete(ind); }else if(self.loop && (self.now_left == -self.width*(ind+1))){ self.complete(ind); }else{ if(ind == -1 || ind == self.size){//循环滚动边界 self.index = ind==-1?(self.size-1):0; self.now_left = ind==-1?0:-self.width*(self.size+1); }else{ self.index = ind; self.now_left = -(self.width*(self.index+(self.loop?1:0))); } self.box.css(this.get_style(1)); setTimeout(function(){ self.complete(ind); },self.scroll_time); } }, complete : self = this; self.busy = false; self.config.callback && self.config.callback(self.index); if(ind==-1){ self.now_left = self.minleft; }else if(ind==self.size){ self.now_left = self.maxleft; } self.box.css(this.get_style(2)); self.auto_scroll(); }, next : (!this.busy){ this.go_index(this.index+1); } }, prev : (!this.busy){ this.go_index(this.index-1); } }, move : changeX = point_x - (this.point_x===null?point_x:this.point_x), changeY = point_y - (this.point_y===null?point_y:this.point_y), marginleft = this.now_left, return_value = false, sin =changeY/Math.sqrt(changeX*changeX+changeY*changeY); this.now_left = marginleft+changeX; this.move_left = changeX<0; if(sin>Math.sin(Math.PI/3) || sin<-Math.sin(Math.PI/3)){//滑动屏幕角度范围:PI/3 -- 2PI/3 return_value = true;//不阻止默认行为 } this.point_x = point_x; this.point_y = point_y; this.box.css(this.get_style(2)); return return_value; }, move_end : function(){ var changeX = this.now_left%this.width,ind; if(this.now_left<this.minleft){//手指向左滑动 ind = this.index +1; }ind = this.index-1; }else if(changeX!=0){ if(this.move_left){//手指向左滑动 ind = this.index+1; }else{//手指向右滑动 ind = this.index-1; } }else{ ind = this.index; } this.point_x = this.point_y = null; this.go_index(ind); }, /* 获取动画样式,要兼容更多浏览器,可以扩展该方法 @int fig : 1 动画 2 没动画 */ get_style : function(fig){ var x = this.now_left , time = fig==1?this.scroll_time:0; return { '-webkit-transition':'-webkit-transform '+time+'ms', '-webkit-transform':'translate3d('+x+'px,0,0)', '-webkit-backface-visibility': 'hidden', 'transition':'transform '+time+'ms', 'transform':'translate3d('+x+'px,0,0)' }; } }); /* 这里对外提供调用接口,对外提供接口方法 next :下一页 prev :上一页 go :滚动到指定页 */ $.mggScrollImg = function(box,config){ var scrollImg = new mggScrollImg(box,config); return {//对外提供接口 next : function(){scrollImg.next();}, prev : function(){scrollImg.prev();}, go : function(ind){scrollImg.go_index(parseInt(ind)||0);} } } })(jQuery)

View Code

参考:(修改了源代码的部分bug:循环参数loop和自动切换参数配置无效;新增滚动时长配置;只有一张图片时不允许切换。)

 

posted on

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • HTML5 进阶系列:拖放 API 实现拖放排序 - _林鑫

    HTML5 进阶系列:拖放 API 实现拖放排序 - _林鑫

    2017-05-02 11:02

  • 调取百度地图接口,实现取自己的实时位置,然后可以在百度地图上添加信息标注 - QISHUANG

    调取百度地图接口,实现取自己的实时位置,然后可以在百度地图上添加

    2017-04-18 10:02

  • 计算机网络——DNS协议的学习与实现 - 学数学的程序猿

    计算机网络——DNS协议的学习与实现 - 学数学的程序猿

    2017-04-16 10:00

  • 前端实现搜索记录功能也就是天猫app历史记录存储方便浏览 - 今天的代码你撸了嘛

    前端实现搜索记录功能也就是天猫app历史记录存储方便浏览 - 今天的代

    2017-04-12 14:00

网友点评