/*
* autocomplete - jquery plugin 1.0
*
* copyright (c) 2014 tyeson, 75655192@qq.com
*
* date 2014-10-30
* revision: $id: jquery-focus.js 2014-10-30 12:50 00 $
*
** div结构:
*
*
*/
;(function($) {
$.fn.focu = function(options) {
$.fn.focu.defaults = {
evt: "click",//两种鼠标切换,"hover","click"
defaultindex: 0,//效果从第几个开始,默认为0
effect: "fade",//效果,effect: fade(渐变) , left(左循环滚动)
titonclassname: "active",//当前位置自动增加的class名称
easing: "swing",//动画效果优化,需要easing插件支持
scrolltime: 500,//过度时间
autotime: 2500,//自动运行间隔
subscript: true,//是否启用下标,下标暂无样式
button: true,//是否启用左右按钮,按钮暂无样式
autoplay: true //是否自动切换
};
return this.each(function() {
var opts = $.extend({},
$.fn.focu.defaults, options);
var $this = $(this),
sul = $this.find("ul"),
sli = sul.find("li"),
swidth = sli.outerwidth(true),
sheight = sli.outerheight(true),
slen = sli.length,
timer = null,
tyebtn = opts.subscript,
tyebtns = opts.button,
autoscroll = opts.autoplay,
index = opts.defaultindex;
//oldindex = index;//储存上一个
if (tyebtn) {
$this.append("");
var sol = $this.find("ol"),
array = [];
for (var i = 0; i < slen; i++) {
array[i]= "";
};
if (slen > 1) {
sol.html(array.join(''));
} else {
sol.html(" ");
};
var soli = sol.find("li");
if (opts.evt == "click") {
soli.bind(opts.evt,function() {
index = $(this).index();
doplay(index);
});
} else if (opts.evt == "hover") {
soli.bind("mouseover",function() {
index = $(this).index();
doplay(index);
});
}
};
if (tyebtns && slen > 1) {
$this.append("");
var tyebtnl = $this.find(".tyebtnl"),
tyebtnr = $this.find(".tyebtnr");
tyebtnl.bind("click",function() {
index--;
doplay(index);
});
tyebtnr.bind("click",function() {
index++;
doplay(index);
})
};
switch (opts.effect) { //如果是滚动,复制头尾滚动子元素
case "left":
sul.css({
"position":
"relative"
});
sul.prepend(sli.first().clone().css({
"position":
"absolute",
"right": -swidth,
"top": 0
}));
sul.append(sli.last().clone().css({
"position": "absolute",
"left": -swidth,
"top": 0
}));
break;
case "top":
sul.css({
"position":
"relative"
});
sul.prepend(sli.first().clone().css({
"position":
"absolute",
"left":0,
"top": -sheight
}));
sul.append(sli.last().clone().css({
"position": "absolute",
"left":0,
"top": -sheight
}));
break;
};
//运动效果
function doplay() {
switch (opts.effect) {
case "fade":
if (index > slen - 1) {
index = 0;
} else if (index < 0) {
index = slen - 1;
}
sli.eq(index).stop(false, true).fadein(opts.scrolltime + 300).siblings().stop(false, true).fadeout(opts.scrolltime);
break;
case "left":
sul.css({
"width":
swidth * slen
});
sli.css({
"float": "left"
});
if (index >= slen) {
$(sul).css("margin-left", swidth);
index = 0;
} else if (index <= -1) {
$(sul).css("margin-left", -slen*swidth);
index = slen - 1;
}
sul.stop(true, false).animate({
marginleft: -index * swidth
},opts.scrolltime, opts.easing);
break;
case "top":
sul.css({
"height":
sheight * slen
});
if (index >= slen) {
$(sul).css("margin-top", sheight);
index = 0;
} else if (index <= -1) {
$(sul).css("margin-top", -slen*sheight);
index = slen - 1;
}
sul.stop(true, false).animate({
margintop: -index * sheight
},opts.scrolltime, opts.easing);
break;
};
if(tyebtn){
soli.eq(index).addclass(opts.titonclassname).siblings().removeclass(opts.titonclassname);
}
//oldindex = index;//记录上一个
};
doplay(); //初始化
//自动播放
if (autoscroll&&slen > 1) {
timer = setinterval(function() {
index++;
doplay()
},
opts.autotime);
$this.hover(function() {
if (autoscroll) {
clearinterval(timer);
}
},function() {
if (autoscroll) {
clearinterval(timer);
timer = setinterval(function() {
index++;
doplay()
},opts.autotime);
}
});
}
});
};
})(jquery);