javascript多张图片切换

原创
2017/09/09 21:53
阅读数 107
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
#pic{
    width:498px;
    height: 498px;
    border:#6f6f71 1px solid;
    position: relative;}
#pic img{
    width:500px;
    height:500px;
}
        #num{
            width: 80px;
            height:30px;
            display: inline-block;
            background-color: rgba(0,0,0,0.5);
            position: absolute;
            top:0;
            left: 230px;
            text-align: center;
        }
        #picDes{
            width: 100%;
            height: 40px;
            background-color: rgba(0,0,0,0.4);
            position: absolute;
            bottom: 0px;
            left: 0px;
        }
    </style>
</head>
<body>
<input type="button" id="order" name="order" value="顺序播放">
<input type="button" id="loop" name="loop" value="循环播放">
<div id="pic"><span id="num"></span><img src="img1/x4.jpg" alt="#" id="img"><div id="picDes"></div></div>
<input type="button" id="forward" name="forward" value="上一张">
<input type="button" id="next" name="next" value="下一张">
</body>
<script>
    var order=document.getElementById("order");
    var loop=document.getElementById("loop");
    var pic=document.getElementById("pic");
    var forward=document.getElementById("forward");
    var next=document.getElementById("next");
    var num=document.getElementById("num");
    var picDes=document.getElementById("picDes");
    var img=document.getElementById("img");

    var arr=["img/x1.jpg","img/x2.jpg","img/x3.jpg","img/x4.jpg"];
    var des=["操场风景","羽毛球运动场","篮球运动场","体育馆"];
    var select=true;
    var n=0;
    order.onclick=function () {
        select=true;

    }
    loop.onclick=function () {
        select=false;

    }
    next.onclick=function () {
        n ++;
        if(n>4){
            if(select){
                n=4;
                alert('已经是最后一张啦');
            }else{
                n=0;
            }
        }
        tranform();

    }
    forward.onclick=function () {
        n --;
        if(n<0){
            if(select){
                n=0;
                alert('已经是第一张啦');
            }else{
                n=5;
            }
        }
        tranform();

    }
    function tranform() {
    num=n+'/'+arr.length;
        img.src=arr[n];
        picDes=des[n];
 pic.innerHTML='<div id="pic"><span id="num">'+num+'</span><img src='+img.src+' alt="#" id="img" style="width: 500px; height: 500px;"><div id="picDes">'+picDes+'</div></div>'
    }
</script>
</html>
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部