Js原生轮播图(缺)
思路:
1.设置好css样式和div结构;
2.
利使用偏移量来控制移动距离offsetLeft
让子盒子居中显示
position: absolute;
top: 50%;
transform: translateY(-50%);
3.左右按键
<
4.图片默认显示为第一张
Var index = 1;
给左右按钮分别注册点击事件
ArrowR.onclick = function(){
Var offset = 内容盒子的偏移量+图片的宽度;
var offset = parseInt(uls.offsetLeft) + 500;
uls.style.left = offset + 'px';
}
ArrowL.onclick = function(){
Var offset = 内容盒子的偏移量+图片的宽度;
var offset = parseInt(uls.offsetLeft) - 500;
uls.style.left = offset + 'px';
}
5.让索引按钮与图片位置一致
当图片到达最后一张;设置为最后一张
index -= 1;
if (index < 1) {
index = 5;
// var offset = parseInt(uls.offsetLeft) + 500;
uls.style.left = -2500 + 'px';
}
show(index);
6.公告一个函数
解决索引按钮的样式
function show(key) {
// console.log(navs);
for (var i = 0; i < arr.length; i++) {
arr[i].children[0].className = 'hidden';
// console.log(sign);
}
arr[key - 1].children[0].className = 'current'
}
7.定时器
timer = setInterval(function () {
arrowL.onclick();
}, 2000)