轮播图(移动端)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>phoneswiper</title>
<style>
body {
padding: 0;
margin: 0;
}
.cont {
position: relative;
width: 100%;
overflow: hidden;
height: 100px;
}
.cont ul {
padding: 0;
margin: 0;
margin-left: -100%;
width: 500%;
height: 100px;
}
.cont ul li {
float: left;
width: 20%;
height: 100px;
list-style: none;
}
.cont ul li div {
width: 100%;
height: 100px;
font-size: 30px;
text-align: center;
line-height: 100px;
background-color: #ccc;
}
.cont ol {
padding: 0;
margin: 0;
position: absolute;
right: 10px;
bottom: 10px;
list-style: none;
}
.cont ol li {
float: left;
width: 10px;
height: 10px;
margin-left: 5px;
background-color: red;
border-radius: 10px;
transition: all .3s;
}
.cont ol .current {
width: 15px;
}
</style>
</head>
<body>
<div class="cont">
<ul>
<li>
<div class="img">3</div>
</li>
<li>
<div class="img">1</div>
</li>
<li>
<div class="img">2</div>
</li>
<li>
<div class="img">3</div>
</li>
<li>
<div class="img">1</div>
</li>
</ul>
<ol>
<li class="current"></li>
<li></li>
<li></li>
</ol>
</div>
<script>
var ul = document.querySelector('ul');
var li = ul.querySelector('li');
var ol = document.querySelector('ol')
var liwidth = li.offsetWidth;
var index = 0;
var timer = setInterval(function() {
index++;
var movex = -index * liwidth;
ul.style.transition = 'all .3s'
ul.style.transform = 'translateX(' + movex + 'px)'
}, 1000)
ul.addEventListener('transitionend', function() {
if (index >= ul.children.length - 2) {
index = 0;
ul.style.transition = 'none'
var movex = -index * liwidth;
ul.style.transform = 'translateX(' + movex + 'px)'
} else if (index < 0) {
index = 2;
ul.style.transition = 'none'
var movex = -index * liwidth;
ul.style.transform = 'translateX(' + movex + 'px)'
}
ol.querySelector('.current').classList.remove('current');
ol.children[index].classList.add('current')
})
var startX = 0;
var move = 0;
var flag = false;
ul.addEventListener('touchstart', function(e) {
startX = e.targetTouches[0].pageX;
clearInterval(timer)
})
ul.addEventListener('touchmove', function(e) {
move = e.targetTouches[0].pageX - startX;
var movex = -index * liwidth + move;
ul.style.transition = 'none'
ul.style.transform = 'translateX(' + movex + 'px)';
flag = true;
e.preventDefault();
})
ul.addEventListener('touchend', function(e) {
if (flag) {
if (Math.abs(move) > 50) {
if (move > 0) {
index--;
} else {
index++;
}
var movex = -index * liwidth;
ul.style.transition = 'all .3s'
ul.style.transform = 'translateX(' + movex + 'px)'
timer = setInterval
} else {
var movex = -index * liwidth;
ul.style.transition = 'all .1s'
ul.style.transform = 'translateX(' + movex + 'px)'
}
}
clearInterval(timer)
timer = setInterval(function() {
index++;
var movex = -index * liwidth;
ul.style.transition = 'all .3s'
ul.style.transform = 'translateX(' + movex + 'px)'
}, 1000)
})
</script>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c3826d051fc858619996114cce37b450.png)