(function($) { app = new function() {}; app.transformval = function(transformval) { return { '-webkit-transform': 'translate(' + transformval + 'px,0)', '-moz-transform': 'translate(' + transformval + 'px,0)', '-o-transform': 'translate(' + transformval + 'px,0)', '-ms-transform': 'translate(' + transformval + 'px,0)', 'transform': 'translate(' + transformval + 'px,0)' }; }; app.throttle = function(fn, delay, mustrundelay) { var timer = null; var t_start; return function() { var context = this, args = arguments, t_curr = +new date(); cleartimeout(timer); if (!t_start) { t_start = t_curr; } if (t_curr - t_start >= mustrundelay) { fn.apply(context, args); t_start = t_curr; } else { timer = settimeout(function() { fn.apply(context, args); }, delay); } }; }; app.tabslider = function(dom, options) { var $this = $(dom); if ($this.length == 0) return this; if ($this.length > 1) { $this.each(function() { app.tabslider($(this), options) }); return this; } if ($this.data('bind')) { return false; } else { $this.data('bind', true); } var opts = $.extend({ auto: false, spend: 3000, threshold: 100, maxwidth: null, type: 'tabs' }, options || {}); var tw, timer, $hd = $this.find("div.tabslider-hd"), $content = $this.find("div.tabslider-bd"), $bd = $this.find("div.tabslider-bd>div.tabslider-wrap"), moved = false, isscrolling, currentindex = 0, minwidth, offset, istouchpad = (/hp-tablet/gi).test(navigator.appversion), hastouch = 'ontouchstart' in window && !istouchpad, start_ev = hastouch ? 'touchstart' : 'mousedown', move_ev = hastouch ? 'touchmove' : 'mousemove', end_ev = hastouch ? 'touchend' : 'mouseup', c_ev = hastouch ? 'tap' : 'click', _init = function() { _setstyle(); $bd.on('' + start_ev + ' ' + move_ev + ' ' + end_ev + '', _eventhandler); $hd.children('li').on(c_ev, _eventclick); if (opts.auto) { _settimer() } }, _setstyle = function() { if (opts.maxwidth) { $this.css({ maxwidth: opts.maxwidth }); } if (opts.type != '') { $hd.addclass(opts.type); $content.addclass(opts.type); } minwidth = $this.width(); $bd.children('div.tabslider-box').each(function(index) { $(this).css(app.transformval(index * minwidth)); }) var index = $bd.children('div.tabslider-box.curr').index(); if (index != 0) { currentindex = index; $bd.css(app.transformval('-' + index * minwidth)); } _contentheight(); }, _eventhandler = function(e) { switch (e.type) { case move_ev: _touchmove(e); break; case start_ev: _touchstart(e); break; case end_ev: _touchend(); break; } }, _touchstart = function(e) { var point = e.touches ? e.touches[0] : e; if ($(e.target).closest($bd).length != 0) { offset = ({ pagex: point.pagex, pagey: point.pagey, x: 0, y: 0 }); if (opts.auto) _cleartimer(); $content.addclass('tabslider-transition'); isscrolling = undefined; moved = true; } }, _touchmove = function(e) { if (!moved) return; var point = e.touches ? e.touches[0] : e; offset.x = point.pagex - offset.pagex; offset.y = point.pagey - offset.pagey; if (typeof isscrolling == 'undefined') { isscrolling = !!(isscrolling || math.abs(offset.x) < math.abs(offset.y)); } if (moved && !isscrolling) { $bd.css(app.transformval(offset.x - currentindex * minwidth)); e.preventdefault(); } }, _touchend = function() { if (!moved || isscrolling) return; var steplength = offset.x <= -opts.threshold ? math.ceil(-offset.x / minwidth) : (offset.x > opts.threshold) ? -math.ceil(offset.x / minwidth) : 0; if (steplength == 1) { if (currentindex < $bd.children('div.tabslider-box').length - 1) { currentindex++; } } else if (steplength == -1) { if (currentindex > 0) { currentindex--; } } _switchto(); if (opts.auto) _settimer(); moved = false; }, _eventclick = function() { currentindex = $(this).index(); _switchto(); }, _switchto = function() { $bd.removeclass('tabslider-transition').addclass('tabslider-animate').css(app.transformval("-" + (minwidth * currentindex))); _contentheight(); _nav(); }, _nav = function() { $hd.children('li').eq(currentindex).addclass('curr').siblings().removeclass('curr'); }, _contentheight = function() { console.info($bd.children('div.tabslider-box').eq(currentindex).height()) var h = $bd.children('div.tabslider-box').eq(currentindex).height(); $content.css('height', h); }, _settimer = function() { if (timer) _cleartimer(); timer = setinterval(function() { _switchto(currentindex >= $hd.children('li').length - 1 ? currentindex = 0 : currentindex += 1); }, opts.spend) }, _cleartimer = function() { clearinterval(timer); timer = null; } _init(); $(window).resize(function() { app.throttle(_setstyle(), 50, 30) }); }; })(zepto);