window.addeventlistener('load', function () { var banner = document.queryselector('.banner'); var prev = document.queryselector('.prev'); var next = document.queryselector('.next'); var circle = document.queryselector('.gd'); var bannerwidth = banner.offsetwidth; var ul = banner.queryselector('ul'); var num = 0; //鼠标移动盒子内让按钮显示 banner.addeventlistener('mouseenter', function () { prev.style.display = 'block'; next.style.display = 'block'; clearinterval(autoplay); autoplay = null;//清除定时器 }) banner.addeventlistener('mouseleave', function () { prev.style.display = 'none'; next.style.display = 'none'; autoplay = setinterval(function () { //手动调用点击事件 next.click(); }, 2000); }); //设置ul的宽度 var nums = ul.children.length; ul.style.width = nums * bannerwidth + 'px'; //动态创建底部小圆点 for (var k = 0; k < nums; k++) { let i = document.createelement('i'); circle.appendchild(i); i.setattribute('index', k); i.addeventlistener('click', function () { num = this.getattribute('index'); animation(ul, -num * bannerwidth); currents(); }); } //为按钮prev,next加事件 circle.children[num].classname = 'current'; //判断有没有执行完动画 var flag = true; next.addeventlistener('click', function () { if (flag) { flag = false; if (num == nums - 1) { animation(ul, 0); num = 0; flag = true; } else { num++; animation(ul, -num * bannerwidth, function () { flag = true; }); } currents(); } }); prev.addeventlistener('click', function () { if (flag) { flag = false; if (num == 0) { animation(ul, -nums * bannerwidth - 1); num = nums - 1; flag = true; } else { num--; animation(ul, -num * bannerwidth, function () { flag = true; }); } currents(); } }); function currents() { for (var a = 0; a < nums; a++) { circle.children[a].classname = ''; } circle.children[num].classname = 'current'; } //无缝播放 var li = ul.children[0].clonenode(true); ul.appendchild(li); //自动播放 var autoplay = setinterval(function () { //手动调用点击事件 next.click(); }, 2000); })