function animate(obj, target, callback) {
clearInterval(obj.timer);
obj.timer = setInterval(function() {
let step = (target - obj.offsetLeft) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
if (obj.offsetLeft == target) {
clearInterval(obj.timer);
callback && callback();
}
obj.style.left = obj.offsetLeft + step + 'px';
}, 15);
}
window.onload = function load(){
let focus = document.querySelector('.focus');
let arrow1 = document.querySelector('.arrow-l');
let arrow2 = document.querySelector('.arrow-r');
focus.addEventListener('mouseenter',function() {
arrow1.style.display = 'block';
arrow2.style.display = 'block';
})
focus.addEventListener('mouseleave',function() {
arrow1.style.display = 'none';
arrow2.style.display = 'none';
})
var ul = focus.querySelector('ul');
var ol = focus.querySelector('.circle');
for(let i = 0;i<ul.children.length;i++) {
let li = document.createElement('li');
li.setAttribute('index',i);
ol.appendChild(li);
ol.children[0].className = 'current';
li.addEventListener('click',function() {
for(let j = 0;j<ol.children.length;j++) {
ol.children[j].className='';
}
this.className = 'current';
let index = this.getAttribute('index');
cnt = index;
let focusWidth = focus.offsetWidth;
animate(ul,-index*focusWidth);
})
}
let fir = ul.children[0].cloneNode(true);
ul.appendChild(fir);
let cnt = 0 ;
let flag = true;
let focusWidth = focus.offsetWidth;
arrow1.addEventListener('click',function(){
if(flag) {
cnt--;
if(cnt < 0) {
cnt = ol.children.length - 1;
ul.style.left = -cnt *focusWidth +'px';
}
animate(ul,-cnt*focusWidth,function() {
flag = true;
});
circleChange(cnt);
}
})
arrow2.addEventListener('click',function(){
if(flag) {
flag = false;
cnt++;
if(cnt == ul.children.length - 1) {
ul.style.left = 0;
cnt = 0;
ol.children[0].className = 'current';
}
animate(ul,-cnt*focusWidth,function() {
flag = true;
});
circleChange(cnt);
}
})
function circleChange(cnt) {
for(let j = 0;j<ol.children.length;j++) {
ol.children[j].className = '';
}
ol.children[cnt].className = 'current';
}
let timer = setInterval(function() {
arrow2.click();
},2000)
}