<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style>
.box{
width: 500px;
height: 400px;
overflow: hidden;
position: relative;
}
.wrap{
width: 2000px;
height: 400px;
position: absolute;
}
.wrap img{
width: 500px;
height: 400px;
font-size: 0;
float: left;
}
</style>
</head>
<body>
<div class="box">
<div class="wrap" style="left: 0;">
<img src="images/1.jpeg" alt="">
<img src="images/4.jpg" alt="">
<img src="images/10.jpg" alt="">
<img src="images/12.jpg" alt="">
</div>
</div>
<script>
var wrap=document.getElementsByClassName('wrap')[0] //获取图片列表
var timeId=setInterval(function(){ //设置定时器,自动轮转
if(wrap.style.left=='-500px'){ //如果列表已移动,则将第一张图片剪切到最后
var img=document.getElementsByTagName('img')[0]
wrap.style.transition=null //剪切的同时将left重新置为0,注意要清除动画效果
wrap.appendChild(img)
wrap.style.left='0px'
}else{
wrap.style.transition='all 0.5s' //设置动画执行时间
var x=tonumber(wrap.style.left) //列表左移
wrap.style.left='-'+(parseInt(x)+500)+'px'
}
},1000)
function tonumber(str){ //获取偏移值
if(str[0]=='-'){
var st=str.slice(1)
}else{
st=str
}
return st
}
</script>
</body>
</html>