/* * 时间 : 2015-1-6 * 功能 : 带响应宽度滑动的幻灯片,包括左右按钮、图标切换,自动切换 * 作者 : pozey * * */ (function($) { $.fn.pztouch = function(options) { var opts = $.extend({},$.fn.pztouch.defaults,options); var box = $(this); var imglist = box.find(opts.imglistclass); var imgs = imglist.find(opts.imgsclass); var downx = 0; var downy = 0; var downleft = 0; var boxwidth = 0; var timer = null; setstyle(); opts.btnleft&&opts.btnright?bindbtnchange():null; opts.btns?bindbtns():null; opts.autoplay?autoplay():null; opts.firstfunc?opts.firstfunc():null; $(window).resize(function(){ setstyle(); }); function setstyle(){ opts.resizefunc?opts.resizefunc():null; opts.outbox.width(""); boxwidth = opts.outbox.width(); box.css({width:boxwidth}); imgs.css({width:boxwidth}); imglist.css({width:boxwidth*imgs.length,left:-boxwidth*opts.ind}); } function autoplay(){ cleartimeout(timer); timer = settimeout(function(){ if(opts.ind0){ opts.ind--; }else{ if(opts.loop){ opts.ind = imgs.length-1; } } changeanimate(); return false; }); opts.btnright.on('click',function(ev){ if(opts.ind'; } opts.btns.html(str).find(opts.btnstype).on(opts.btnsevent,function(ev){ cleartimeout(timer); opts.ind = $(this).index(); changeanimate(); return false; }).eq(opts.ind).addclass("cur"); } function changeanimate(){ imglist.stop(false,false).animate({left:-parseint(box.css("width"))*opts.ind},300,function(){ opts.autoplay?autoplay():null; if($.fn.pztouch.defaults.ind != opts.ind){ $.fn.pztouch.defaults.ind = opts.ind; opts.endfunc?opts.endfunc():null; } }); opts.btnstype?opts.btns.find(opts.btnstype).removeclass("cur").eq(opts.ind).addclass("cur"):null; } imglist.on('touchstart',function(ev){ var touchs = ev.originalevent.changedtouches[0]; downx = touchs.pagex; downy = touchs.pagey; downleft = parseint(imglist.css("left")); var onf = true; imglist.on('touchmove',function(ev){ var touchs = ev.originalevent.changedtouches[0]; if(math.abs((downx-touchs.pagex))>math.abs((downy-touchs.pagey))){ document.ontouchmove = function(ev){ ev.preventdefault(); }; }else{ imglist.off('touchmove'); return; } if(parseint(imglist.css("left")) >= 0){ if(onf){ onf = false; downx = touchs.pagex; } imglist.css({"left":(touchs.pagex - downx)/3}) }else if(parseint(imglist.css("left")) <= parseint(box.css("width")) - parseint(imglist.css("width"))){ if(onf){ onf = false; downx = touchs.pagex; } imglist.css({"left":(touchs.pagex - downx)/3 + (parseint(box.css("width")) - parseint(imglist.css("width")))}) }else{ imglist.css({"left":(touchs.pagex - downx + downleft)}) } }); imglist.on('touchend',function(ev){ var touchs = ev.originalevent.changedtouches[0]; if(touchs.pagex < downx){ if(opts.ind!=imgs.length-1){ if(downx - touchs.pagex > 20){ opts.ind++; } } changeanimate(); }else{ if(opts.ind!=0){ if(touchs.pagex - downx > 20){ opts.ind--; } } changeanimate(); } imglist.off('touchmove'); imglist.off('touchend'); document.ontouchmove = null; }); }); return this; } $.fn.pztouch.defaults = { imglistclass : "ul", imgsclass : "li", outbox : $(window), btnleft : null, btnright : null, btns : null, btnstype : null, btnsevent : "click", autoplay : false, autospeed : 500, loop: true, firstfunc : null, resizefunc : null, endfunc : null, ind : 0 }; })(jquery); //$(function(){ // // $(".touchbox").pztouch({ // imglistclass : "ul",//被移动的框 // imgsclass : "li",//每一项 // outbox : $(".box"),//父级宽度基于此外框 // btnleft : $(".btnleft"),//左右切换按钮 // btnright : $(".btnright"), // btns : $(".btns"),//按钮父级 // btnstype : "span",//按钮类型 // btnsevent : "mouseover",//按钮事件 // autoplay : false,//自动播放开关 // autospeed : 5000,//自动播放间隔时间 // firstfunc : null,//初始化执行一次 // resizefunc : null,//改变窗口时执行 // endfunc : null,//滑动结束回调 // ind : 0//初始索引 // }); // $.fn.pztouch.defaults.ind //获得索引 //});