实现效果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#box{
width: 98%;
height: 84vh;
margin-top:20vh;
}
span{
width: 5%;
height: 10%;
display: inline-block;
}
.active{
background-color: #ddd;
}
</style>
</head>
<body>
<div id="box"></div>
</body>
</html>
<script>
var index,arr,NewSelect;
init();
function EmptyFun(){
index=0;
arr = [];
NewSelect=null;
}
function init(){
EmptyFun();
var innerHtml = '';
for(var i =0;i<100;i++){
var obj = {
value:i+1,
index:i
};
arr.push(obj);
innerHtml+='<span id="span_'+obj.index+'" onclick="spanClick('+obj.index+')">'+obj.value+'</span>';
}
document.getElementById('box').innerHTML = innerHtml;
if(arr.length>0){
activeSpan(0);
var timer = setInterval(function(){
moverleft(index);
},1000);
}
}
function spanClick(order){
debugger;
index = order;
activeSpan(order);
}
function moverleft(order){
index = order+1;
if(index>=arr.length)index=0;
activeSpan(index);
}
function activeSpan(next){
if(NewSelect!==null){
NewSelect.className='';
}
NewSelect = document.getElementById("span_"+next);
if(NewSelect!==null){
NewSelect.className='active';
}
if(next>98){
// debugger;
}
var obj = arr[next];
console.log(obj.value);
}
</script>