好了废话不多说了,先看看效果图。
HTML部分:
CSS部分:
JS部分:
var _this = $(this),ulBox = $('.ul-Box',this),ul = $('ul',li = $('li',ul),img = $('img',li),len = li.size(),mask = $('.mask',index = 0,//用来控制按钮的下标
flag = true,//标识动画是否执行完成
gap,//执行动画的按钮下标和当前按钮的下标的差
timer; //计时器
_this.width(setting.width).height(setting.height);
ulBox.width(setting.width 3 len).height(setting.height).css({
marginLeft: -setting.width len
});
ul.width(setting.width len).height(setting.height);
img.width(setting.width).height(setting.height);
mask.height(setting.btnSize + 2 * setting.btnSpace);
//添加btn
for(var i = 0,str = ''; i < len; i++){
str += '<div class="sliderBtn">
}
mask.html(str);
var ulFir = ul.clone(true);
var ulSec = ul.clone(true);
var sliderBtn = $('.sliderBtn','.mask');
ulBox.append(ulFir);
ulBox.append(ulSec);
sliderBtn.each(function (i,item) {
$(item).css({
width : setting.btnSize,height : setting.btnSize,top : setting.btnSpace,left : parseInt(setting.width - (setting.btnSize + setting.btnSpace 2) len,10) /2 + setting.btnSize i + setting.btnSpace i 2 + setting.btnSpace
});
$(item).mouseenter(function () {
gap = Math.abs(i - index);
i > index ? (flag && ani(1,gap)) : (flag && ani(0,gap));
})
});
btnAni(0);
//按钮样式变化
function btnAni (index) {
$('.sliderBtn').css('background','radial-gradient(white 20%,transparent 50%)');
$('.sliderBtn').eq(index).css('background','radial-gradient(white 10%,transparent 30%,white 70%)');
}
//动画
function ani (direction,num) {
if(flag){
//默认1向左运动,0向右运动
flag = false;
var ulfir = $('ul',ulBox).eq(0);
var left = parseInt(ulfir.css('margin-left'),10);
if(direction){
index += num;
index = index % len;
left = left - num setting.width;
ulfir.animate({
marginLeft: left
},setting.speed,function () {
if(left <= -len setting.width){
ulfir = ulfir.remove();
ulfir.css('margin-left',0);
ulBox.append(ulfir);
}
flag = true;
});
}else{
index -= num;
index = index <= -1 ? len - 1 : index;
left = left + num setting.width;
ulfir.animate({
marginLeft: left
},function () {
if(left >= len * setting.width){
var ulLast = $('ul',ulBox).eq(2).remove();
ulLast.css('margin-left',0);
ulBox.prepend(ulLast);
ulfir.css('margin-left',0);
}
flag = true
})
}
btnAni(index)
}
}
//定时器
timer = setInterval(ani,setting.during,setting.direction,1);
_this.hover(function () {
$('.prev',this).css('display','block');
$('.next','block');
clearInterval(timer);
},function () {
$('.prev','none');
$('.next','none');
timer = setInterval(ani,1);
})
$('.next').click(function () {
flag && ani(1,1);
})
$('.prev').click(function () {
flag && ani(0,1);
})
})
}
})(jQuery)
以上就是差不多所有的代码了,一些比较关键的地方已经用注释好了,这里就不再叙述。
原文链接:https://www.f2er.com/jquery/47841.html