首页 > 代码库 > 图片切换(练习)

图片切换(练习)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.demo{
width:200px;
height: 140px;
overflow:hidden;
position: relative;
}
.demo .imgs
{
position: absolute;
width:600px;
height:140px;

}
.demo .imgs img{
width:200px;
float: left;
}
</style>

</head>
<body>
<div id="demo" class="demo">
<div id="imgs" class="imgs">
<img src="http://www.mamicode.com/images/t1.jpg" >
<img src="http://www.mamicode.com/images/t2.jpg" >
<img src="http://www.mamicode.com/images/t3.jpg" >
</div>
</div>
<button id="prev"></button><button id="next"></button>
<script>
function $(id){
return document.getElementById(id);
}
var demo=$(‘demo‘);
var imgs=$(‘imgs‘);
var prev=$(‘prev‘);
var next=$(‘next‘);
var images=imgs.getElementsByTagName(‘img‘);
var n=0;
next.onclick=function(){
if(n<images.length-1){
n++;
}
else{
n=images.length-1;
}

imgs.style.left=-n*200+"px";
}

prev.onclick=function(){
if(n>0){
n--;
}
else{
n=0;
}

imgs.style.left=-n*200+"px";
}

</script>
</body>
</html>

图片切换(练习)