<!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>Document</title>
</head>
<style>
#container {
width: 550px;
height: 400px;
margin: 50px auto;
overflow: hidden;
position: relative;
}
#list {
width: 1100px;
height: 300px;
position: relative;
transition: all .3s;
}
img {
width: 550px;
height: 300px;
float: left;
}
.arrow {
display: inline-block;
width: 100px;
height: 30px;
text-align: center;
line-height: 30px;
margin-top: 20px;
background: plum;
cursor: pointer;
}
#next {
float: right;
}
</style>
<body>
<div id="container">
<div id="list" style="left: 0px">
<img src="./1.jpg" alt="">
<img src="./2.jpg" alt="">
</div>
<span id="prev" class="arrow" onclick="last()">《</span>
<span id="next" class="arrow" onclick="next()">》</span>
</div>
<script>
var list = document.getElementById('list');
var num = parseInt(list.style.left);
//上一个
function last() {
if (num == 0) {
alert('已经是第一张了')
return;
}
num += 550;
list.style.left = num + 'px';
}
// 下一个
function next() {
if (num == -550) {
alert('已经是最后一张了')
return;
}
num -= 550;
list.style.left = num + 'px';
}
</script>
</body>
</html>