87,922
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
div,div>img{
position: relative;
width: 200px;
height: 200px;
}
div>span{
position: absolute;
top: 50%;
z-index: 2;
}
div>span:nth-of-type(1){
left: 0;
}
div>span:nth-of-type(2){
right: 0;
}
img:before{
position: absolute;
top: 50%;
left: 0;
width: 100%;
height: 100%;
text-align: center;
content: attr(src);
}
</style>
</head>
<body>
<div>
<span class="js_left"> < </span>
<img src=""/>
<span class="js_right"> > </span>
</div>
<script>
const arr=['a','b','c','d'],len=arr.length;
const img=document.querySelector('img');
let index=0;
document.body.onclick=function(e){
var target= e.target;
if(target.classList.contains('js_left')){
index--;
if(index<0) index+=len;
}else if(target.classList.contains('js_right')){
index++
if(index>=len) index%=len;
}
img.src=arr[index]+'.jpg';
}
</script>
</body>
</html>
类似这样试试