分页
程序员文章站
2022-05-28 12:21:34
...
<style>
#box{width:1200px;margin:0 auto;}
p{text-align:center;}
p .next{margin-left: 20px}
</style>
<body>
<div id="box" align="center">
<img src="img/1.jpg" id="img">
<p>
<a class="prev btn">上一页</a>
<a class="next btn">下一页</a>
</p>
</div>
<script src="js/jquery-1.7.2.min.js"></script>
<script>
var imgs=[1,2,3,4];
var length=imgs.length;
var index=0;
$(".btn").on("click",function(){
if($(this).attr("class")=="prev btn"){//上一张
if(index>0){
index--;
$("#img").attr("src","img/"+imgs[index]+".jpg")
}else if(index<=0){
$("#img").attr("src","img/"+imgs[3]+".jpg");
index=3;
}
}else{//下一张
if(index<3){
index++;
$("#img").attr("src","img/"+imgs[index]+".jpg");
}else if(index>=3){
$("#img").attr("src","img/"+imgs[0]+".jpg");
index=0;
}
}
})
</script>
</body>