<body>
<div class="content">
<div id="list">
<img src="images/01.png" alt="">
<img src="images/02.png" alt="">
<img src="images/03.png" alt="">
<img src="images/04.png" alt="">
<img src="images/05.png" alt="">
</div>
<button id="prev"></button>
<button id="next"></button>
<div id="btns">
<span class="current"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
<script>
$(function () {
var index = 0;
// 1.点击next
$("#next").click(function () {
index++;
if (index > 4) {
index = 0;
}
animate(index);
})
// 2.点击prev
$("#prev").click(function () {
index--;
console.log(index);
if (index < 0) {
index = 4;
}
animate(index);
})
// 3.焦点随左右按钮变化
function animate(index) {
$("#list img").eq(index).fadeIn(300).siblings().fadeOut(300);
$("#btns span").eq(index).addClass("current").siblings().removeClass("current");
}
// 4.点击按钮,让对应的图片出现
$("#btns span").click(function(){
$(this).addClass("current").siblings().removeClass("current");
index = $(this).index();
$("#list img").eq(index).fadeIn(300).siblings().fadeOut(300);
})
})
</script>
</body>
方法2
<style>
.parent{
position: relative;
margin-left: 300px;
}
.parent>div{
width: 600px;
height: 400px;
position: absolute;
}
.parent>div img{
width: 100%;
height: 100%;
}
ul{
overflow: hidden;
transform: translateY(450px);
margin-left: 280px;
}
ul li{
float: left;
width: 60px;
margin-left: 100px;
}
.li1{
color: red;
}
</style>
<body>
<div class="parent">
<div><img src="./images/351.jpg" alt=""></div>
<div><img src="./images/352.jpg" alt=""></div>
<div><img src="./images/353.jpg" alt=""></div>
</div>
<ul>
<li class="li1">1</li>
<li>2</li>
<li>3</li>
</ul>
<script>
var divs = document.querySelectorAll(".parent div");
var lis = document.querySelectorAll("ul li");
for(var i=0;i<lis.length;i++){
lis[i].index = i;
lis[i].onclick = function(){
for(var i=0;i<lis.length;i++){
lis[i].className="";
}
this.className="li1";
for(var i=0;i<lis.length;i++){
divs[i].style.display = "none"
}
divs[this.index].style.display="block"
}
}
</script>