<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style type="text/css">
.main{
width: 500px;
height: 500px;
}
#container {
position: relative;
width: 550px;
height: 252px;
border: 3px solid #333;
overflow: hidden;
}
#list {
position: absolute;
z-index: 1;
width: 1100px;
height: 252px;
display:flex;
}
#list img {
width: 550px;
}
.arrow {
display:block;
position: absolute;
top: 105px;
z-index: 2;
display: none;
width: 20px;
height: 40px;
font-size: 36px;
font-weight: bold;
line-height: 37px;
text-align: center;
color: #fff;
cursor: pointer;
}
#container:hover .arrow {
display: block;
}
#prev {
left: 3px;
}
#next {
right: 20px;
}
</style>
</head>
<body>
<div id="container">
<div id="list" style="left:0px;">
<img src="res/htmlLX/gunbo1.jpg" />
<img src="res/htmlLX/gunbo2.jpg" />
</div>
<span id="prev" class="arrow" onclick="last()">《</span>
<span id="next" class="arrow" onclick="next()">》</span>
</div>
<script>
var list = document.getElementById('list');
//上一个
function last() {
var a = parseInt(list.style.left)
if(a === 0){
list.style.left = -550 + "px";
}
else if(a = -550){
list.style.left = 0 + "px";
}
}
//下一个
function next() {
var a = parseInt(list.style.left)
if(a === 0){
list.style.left = -550 + "px";
}
else if(a = -550){
list.style.left = 0 + "px";
}
}
//第二个方法
function last() {
var newleft = parseInt(list.style.left) + 550;
list.style.left = newleft + "px";
if(newleft > 0){
list.style.left = -550 + "px";
}
}
function next() {
var newleft = parseInt(list.style.left) - 550;
list.style.left = newleft + "px";
if(newleft < -550){
list.style.left = 0 + "px";
}
}
</script>
</body>
</html>