js实现图片切换

这里写图片描述
说明:两种浏览模式
循环切换模式:通过鼠标点击左键或右键可一直循环切换显示图片。
顺序切换模式:通过鼠标点击左键或右键只能从第一张切换到最后一张或从最后一张切换到第一张。
html代码:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>图片切换</title>
<link rel="stylesheet" type="text/css" href="qiehuan.css">
<script>
window.onload=function(){
    var oPrev=document.getElementById("prev");
    var oNext=document.getElementById("next");
    var oP=document.getElementById("p1");
    var oStrong=document.getElementById("strong1");
    var oImg=document.getElementById("img1");
    var oInput=document.getElementsByTagName("input");

    var arrUrl=['1.jpg','2.jpg','3.jpg','4.jpg'];
    var arrText=['文字一','文字二','文字三','文字四'];
    var num = 0;
    var onOff = true;

    oInput[0].onclick=function(){
        onOff=true;
        document.getElementsByTagName('span')[0].innerHTML='图片可从最后一张跳转到第一张循环切换';
    };

    oInput[1].onclick=function(){
        onOff=false;
        document.getElementsByTagName('span')[0].innerHTML='图片只能到最后一张切换/或到第一张切换';
    }; 

    oPrev.onclick=function(){
        num--;
        if(num==-1){
            if(onOff){
                num=arrText.length-1;
            }else{
                alert('这已经是第一张了不能往前了~');
                num=0;
            }   
        }
        fnTab();
    };

    oNext.onclick=function(){
        num++;
        if(num==arrText.length){
            if(onOff){
                num=0;
            }else{
                alert('这已经是最后一张了');
                num=arrText.length-1;
            }   
        }
        fnTab();
    };

    //初始化
    function fnTab(){
        oP.innerHTML=arrText[num];
        oStrong.innerHTML=num+1+'/'+arrText.length;
        oImg.src=arrUrl[num];
    }
    fnTab();
};
</script>
</head>

<body>
<input type="button" value="循环切换"/>
<input type="button" value="顺序切换"/>

<div id="box" class="box">
<span>图片可从最后一张到第一张循环切换</span>
<a id="prev" class="prev" href="javascript:;"><</a>
<a id="next" class="next" href="javascript:;">></a>
<p id="p1" class="p1">图片文字加载中...</p>
<strong id="strong1" class="strong1">图片数量计算中...</strong>
<img id="img1" />
</div>
</body>
</html> 

css代码:

p{
margin:0;
}
body{
text-align:center;
}

.box{
width:400px;
height:400px;
border:10px solid #ccc;
margin:50px auto ;
position:relative;
}

a{
width:40px; 
height:40px; 
background:#000; 
filter:alpha(opacity:80); 
opacity:0.8; 
position:absolute; 
top:160px; 
font-size:18px; 
color:#fff;
text-align:center; 
line-height:40px; 
text-decoration:none; 
}
a:hover { 
background-color:silver;
}
.prev{
left:10px;
}
.next{
right:10px;
}

span { 
position:absolute; 
width:400px; 
height:30px; 
line-height:30px; 
text-align:center; 
top:-50px; 
left:0; 
font-family:'微软雅黑'; 
}

.p1 { 
width:400px;
height:30px; 
line-height:30px;
text-align:center; 
background:#000; 
color:#fff; 
font-size:14px; 
position:absolute; 
bottom:0; 
left:0; 
filter:alpha(opacity:80); 
opacity:0.8;
}
.strong1 { 
width:400px; 
height:30px; 
line-height:30px; 
text-align:center; 
background:#000; 
color:#fff; 
font-size:14px; 
filter:alpha(opacity:80); 
opacity:0.8;
position:absolute; 
top:0; 
left:0; 
}
#img1 { 
width:400px; 
height:400px; 
}
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值