/**
* [ xtaber 切换 ]
* @param {[type]} $ [description]
* @return {[type]} [description]
*/
(function($){
$.fn.extend({
xtaber: function(opt){
var def = $.extend({
/* @tab 触发事件标签 [true|false|obj]
* true 自动生成带数字的标签
* false 不显示tab
* obj 自定义tab
*/
tab: true, //默认为自动生成
content:$('#xtaberwrap'),
prev: null, //上一个按钮
next: null, //下一个按钮
/* @style 滚动的样式 [opacity|left|top|none]
* opacity 淡出淡入
* left 向左
* top 向上
* none 无效果
*/
style: 'opacity', //默认为opacity
activeclass: 'current', //当前样式
delay: 100, //操作延时
speed: 300, //移动速度
timeout: 3000, //间歇时间
auto: false, //是否自动滚动
setup: 1,//每次滚动多少个
defaultshow: 1, //默认显示第n个
mouseevent: 'mouseover', //鼠标事件
tabedcallback: null //切换后的回调函数
}, opt);
if(typeof def.setup != number && def.setup < 1) def.setup = 1;
// 内部通用变量
var self = def.content,
content = self.find('[rel="xtaberitems"]'),
subitem = content.find('.xtaber-item'),
itemlength = subitem.length,
subitemheight =
parseint(subitem.height())+
parseint(subitem.css('margintop'))+
parseint(subitem.css('marginbottom'))+
parseint(subitem.css('paddingtop'))+
parseint(subitem.css('paddingbottom')),
subitemwidth =
parseint(subitem.width())+
parseint(subitem.css('marginleft'))+
parseint(subitem.css('marginright'))+
parseint(subitem.css('paddingleft'))+
parseint(subitem.css('paddingright')),
scrollheight = subitemheight * def.setup,
scrollwidth = subitemwidth * def.setup,
screennum,
current = 0,
autotimer,
itemtimer,
tabitem = null;
//滚动屏数
if(def.setup == 1){
screennum = itemlength;
}
else{
var inall = (itemlength % def.setup),
num = parseint(itemlength / def.setup);
screennum = (inall > 0) ? (num+1) : num;
}
var init = function(){
// 自动生成tab
if(def.tab && typeof def.tab != 'object'){
var tabhtml = '
';
for(var i=1; i<=screennum; i++){
tabhtml += '- '+i+'
';
}
tabhtml += '
';
self.append(tabhtml);
def.tab = self.find('[rel="xtabertabs"]');
}
else if(typeof def.tab == 'object'){
def.tab = self.find('[rel="xtabertabs"]');
}
else{
def.tab = null;
}
if(def.tab != null){
tabitem = def.tab.find('[rel="xtabertabitem"]');
}
if(typeof def.next == 'boolean' && def.next){
def.next = $('next');
def.next.appendto(self);
}
if(typeof def.prev == 'boolean' && def.prev){
def.prev = $('prev');
def.prev.appendto(self);
}
switch(def.style){
case 'left':
setparent('left');
break;
case 'top':
setparent('top');
break;
}
goto(def.defaultshow - 1);
bindevent();
if(def.auto){
auto();
}
}
//设置父级的样式
var setparent = function(type){
var wrapheight,wrapwidht,contentwidth,contentheight;
if(type == 'top'){
contentheight = subitemheight * itemlength;
contentwidth = subitemwidth;
}
else if(type == 'left'){
contentheight = subitemheight;
contentwidth = subitemwidth * itemlength;
}
//alert(typeof(subitemwidth));
content.css({
left: 0,
top: 0,
position: 'absolute',
width: contentwidth,
height: contentheight
});
}
var goto = function(index){
clearinterval(autotimer);
cleartimeout(itemtimer);
current = index;
switch(def.style){
case 'top':
content.stop().animate({'top': -(index * scrollheight)}, def.speed);
break;
case 'left':
content.stop().animate({'left': -(index * scrollwidth)}, def.speed);
break;
case 'opacity':
subitem.eq(index).fadein().siblings().hide();
break;
default:
subitem.eq(index).show().siblings().hide();
break;
}
if(def.tab != null){
tabitem.eq(index).addclass(def.activeclass).siblings().removeclass(def.activeclass);
}
if(def.auto){
auto()
};
if(def.tabedcallback){
tabedcallback();
}
}
var auto = function(){
if(def.auto){
clearinterval(autotimer);
autotimer = setinterval(function(){
if(current + 1 >= screennum){
goto(0);
}else{
goto(current + 1);
}
}, def.timeout);
}
}
//绑定事件
var bindevent = function(){
if(def.tab != null){
tabitem.each(function(){
var el = $(this);
el.bind(def.mouseevent, function(){
clearinterval(autotimer);
cleartimeout(itemtimer);
itemtimer = settimeout(function(){
goto(el.index());
}, def.delay);
});
el.focus(function(){
clearinterval(autotimer);
cleartimeout(itemtimer);
itemtimer = settimeout(function(){
goto(el.index());
}, def.delay);
});
el.bind('mouseleave', function(){
cleartimeout(itemtimer);
auto();
});
});
}
if(def.next){
def.next.click(function(){
var currentnum = (current + 1 >= screennum) ? 0 : current + 1;
goto(currentnum);
});
}
if(def.prev){
def.prev.click(function(){
var currentnum = (current - 1 < 0) ? screennum - 1 : current - 1;
goto(currentnum);
});
}
}
init();
}
});
})(jquery);
var isneeded = function(selectors){
var selectors = (typeof selectors == 'string') ? [selectors] : selectors,
isneeded;
for(var i=0;i 0 ) {
isneeded = true;
break;
}
};
return isneeded ;
};