1. 程式人生 > >js簡單的通過點選“上一張”“下一張”圖片切換

js簡單的通過點選“上一張”“下一張”圖片切換

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=GBK" />
<title></title>
//8張圖片1.png2.png3.png.............
<script type = "text/javascript">
var now = 1;
function changeImg(which){

var img = document.getElementById("images");
if(which == 'pre'){
now--;
if(now < 1){

now = 8;
img.src = now+".png"
}
img.src = now+".png";


}else{

now++;
if(now > 8){
now = 1;
img.src = now+".png";
}
img.src = now+".png";

}


}

</script>
</head>
<body>
<div id = "changeImg">
<img id = "images" src = "1.png"/>
</div>
<div>
<input type = "button" value = "上一張" onclick = "changeImg('pre')"/>
<input type = "button" value = "下一張" onclick = "changeImg('next')"/>

</div>
</body>
</html>