console
window.addEventListener('load', function () {
var focus = document.querySelector('.focus');
console.log(focus);
var ul = focus.children[0];
console.log(ul);
var w = focus.offsetWidth;
console.log(w);
var ol = focus.children[1];
console.log(ol);
var index = 0;
var timer = setInterval(function () {
index++;
var translatex = -index * w;
ul.style.transition = 'all .3s';
ul.style.transform = 'translateX(' + translatex + 'px)';
}, 2000);
ul.addEventListener('transitionend', function () {
console.log(index);
if (index >= 3) {
index = 0;
ul.style.transition = 'none';
var translatex = -index * w;
ul.style.transform = 'translateX(' + translatex + 'px)';
} else if (index < 0) {
console.log(index + '213214');
index = 2;
ul.style.transition = 'none';
var translatex = -index * w;
ul.style.transform = 'translateX(' + translatex + 'px)';
}
ol.querySelector('.current').classList.remove('current');
ol.children[index].classList.add('current');
})
var startX = 0;
var moveX = 0;
flag = false;
ul.addEventListener('touchstart', function (e) {
startX = e.targetTouches[0].pageX;
console.log(startX);
clearInterval(timer);
});
ul.addEventListener('touchmove', function (e) {
moveX = e.targetTouches[0].pageX - startX;
console.log(moveX);
var translatex = -index * w + moveX;
ul.style.transform = 'translateX(' + translatex + 'px)';
ul.style.transition = 'none';
flag = true;
})
ul.addEventListener('touchend', function (e) {
if (flag) {
if (Math.abs(moveX) > 50) {
if (moveX > 0) {
index--;
} else {
index++;
}
var translatex = -index * w;
ul.style.transition = 'all .3s';
ul.style.transform = 'translateX(' + translatex + 'px)';
}
}
clearInterval(timer);
var timer = setInterval(function () {
index++;
var translatex = -index * w;
ul.style.transition = 'all .3s';
ul.style.transform = 'translateX(' + translatex + 'px)';
}, 2000);
});
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/index.css">
<title>携程在手,说走就走</title>
<script src="js/index.js"></script>
</head>
<body>
<div class="goBack"></div>
<div class="focus">
<ul>
<li>3</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>1</li>
</ul>
<ol>
<li class="current"></li>
<li></li>
<li></li>
</ol>
</div>
</body>
</html>
body {
max-width: 540px;
min-width: 320px;
margin: 0 auto;
font: normal 14px/1.5 Tahoma, "Lucida Grande", Verdana, "Microsoft Yahei", STXihei, hei;
color: #000;
background: #f2f2f2;
overflow-x: hidden;
-webkit-tap-highlight-color: transparent;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #222;
}
div {
box-sizing: border-box;
}
.goBack {
display: none;
position: fixed;
bottom: 50px;
right: 20px;
width: 38px;
height: 38px;
background: url(../images/back.png) no-repeat;
background-size: 38px 38px;
}
.focus {
position: relative;
padding-top: 44px;
overflow: hidden;
}
.focus img {
width: 100%;
}
.focus ul {
overflow: hidden;
width: 500%;
margin-left: -100%;
}
.focus ul li {
float: left;
width: 20%;
display: block;
height: 100px;
background: rgb(69, 153, 236);
}
.focus ol {
position: absolute;
bottom: 5px;
right: 5px;
margin: 0;
}
.focus ol li {
display: inline-block;
width: 5px;
height: 5px;
background-color: #fff;
list-style: none;
border-radius: 2px;
transition: all .3s;
}
.focus ol li.current {
width: 15px;
}