Skip to content

Instantly share code, notes, and snippets.

@mingyun
Created December 28, 2013 08:49
Show Gist options
  • Save mingyun/8157448 to your computer and use it in GitHub Desktop.
Save mingyun/8157448 to your computer and use it in GitHub Desktop.
/*!
* == jQuery simpleTabs v2.2 ==
* QQ: 390514201[sole]
* 发布: 2012-09-25
* 更新: 2013/10/7 17:48
*/
;(function($){
$.fn.simpleTabs = function(o){
var defs = {
control : ".control a",
target : "",
current : "now",
action : "mouseover",
easing : "toggle",// toggle fade updown left top leftMar topMar leftLoop topLoop
scroll : 1,
autoPlay : 1,
speed : 3000,
time : 400,
btnPrev : ".prev",
btnNext : ".next"
};
var opt = $.fn.extend({}, defs, o);
return this.each(function(){
var target = $(opt.target, $(this));
var control = $(opt.control ,$(this));
var prev = $(opt.btnPrev, $(this));
var next = $(opt.btnNext, $(this));
var now = opt.current;
var action = opt.action;
var scroll = opt.scroll;
var easing = opt.easing;
var autoPlay = opt.autoPlay;
var speed = opt.speed;
var time = opt.time;
var t = target.parent();
var len = t.children().length;
var timerInt = null;
var timerOut = null;
var num = 0;
var n = function(s){
var r = s==1 ? t.children().eq(0).outerHeight(true) : t.children().eq(0).outerWidth(true);
return r;
};
var x = -2;
if(easing == "leftMar" || easing == "topMar" || easing == "leftLoop" || easing == "topLoop"){
t.html(t.html()+t.html());
}
control.eq(0).addClass(now).siblings().removeClass(now);
$.each(control, function(i){
$(this).bind(action, function(){
clearInterval(timerOut);
num = i;
timerOut = setTimeout(fnMove, 200);
});
});
t.mouseover(function(){
clearInterval(timerInt);
}).mouseout(function(){
if(autoPlay){
timerInt = setInterval(function(){
num++;
if(num >= len) num=0;
fnMove();
}, speed);
}
});
if(autoPlay){
timerInt = setInterval(function(){
num++;
if(num >= len) num=0;
fnMove();
}, speed);
}
next.click(function(){
clearInterval(timerOut);
num = control.parent().children("."+now).index();
num++;
if(num >= len) num=0;
timerOut = setTimeout(fnMove, 200);
});
prev.click(function(){
clearInterval(timerOut);
num = control.parent().children("."+now).index();
num--;
if(num < 0) num=len-1;
timerOut = setTimeout(fnMove, 200);
});
var fnMove = function(){
control.eq(num).addClass(now).siblings().removeClass(now);
switch (easing) {
case "toggle": target.stop(true,true).eq(num).show().siblings().hide(); break;
case "fade": target.stop(true,true).eq(num).fadeIn().siblings().hide(); break;
case "updown": target.stop(true,true).eq(num).slideDown().siblings().slideUp(); break;
case "top": t.css("height",n(1)*len); t.stop(true,true).animate({"top":-n(1)*num}, time); break;
case "left": t.css("width",n()*len); t.stop(true,true).animate({"left":-n()*num}, time); break;
case "topMar": fnTopMar(); break;
case "leftMar" : fnLeftMar(); break;
case "leftLoop" : fnLeftLoop(); break;
case "topLoop" : fnTopLoop(); break;
default: alert("sorry, without this parameter!\ndefaults:toggle | fade | updown | top | left | topMar | leftLoop | topLoop");
}
}
var fnLeftMar = function(){
t.css("width",n()*len*2);
var left = parseInt(t.css("left"));
if(-left >= n()*len){
t.css("left","0");
left = 0;
}
t.animate({"left": left-n()*scroll}, time);
}
var fnTopMar = function(){
t.css("height",n(1)*len*2);
var top = parseInt(t.css("top"));
if(-top >= n(1)*len){
t.css("top",0);
top = 0;
}
t.animate({"top":top-n(1)*scroll}, time);
}
var fnLeftLoop = function(){
var left = parseInt(t.css("left"));
next.click(function(){
x = 2
});
prev.click(function(){
x = -2
});
t.css("width",n()*len*2);
clearInterval(timerInt);
timerInt = setInterval(function(){
left = left + x;
if(-left >= n()*len){
t.css("left","0");
left = 0;
}else if(left >= 0){
t.css("left",0);
left = -n()*len;
}
t.css("left", left);
}, speed);
}
var fnTopLoop = function(){
var top = parseInt(t.css("top"));
next.click(function(){
x = 2
});
prev.click(function(){
x = -2
});
t.css("height",n()*len*2);
clearInterval(timerInt);
timerInt = setInterval(function(){
top = top + x;
if(-top >= n(1)*len){
t.css("top","0");
top = 0;
}else if(top > 0){
t.css("top", -n(1)*len);
top = -n(1)*len;
}
t.css("top", top);
}, speed);
}
});
}
$.fn.simpleTabs.version = "2.2";
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment