JS实现的图片选择顺序切换和循环切换功能示例【测试可用】

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

JS实现的图片选择顺序切换和循环切换功能示例【测试可用】

belong_me   2020-05-16 我要评论

本文实例讲述了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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>图片切换</title>
<style type="text/css">
#box{width:400px;margin:0 auto;}
#box>div{text-align: center;margin:10px auto;display: block;}
#box div>input{margin-left: 5px;border-radius: 3px;border:1px solid #ccc;}
#box>p{text-align: center;}
#content{width:400px;height:400px;margin:0 auto;position: relative;border:10px solid #ccc;}
#content a{width:40px;height:40px;background:#000;border:5px solid #fff;position: absolute;top:175px;text-align: center;text-decoration: none;line-height: 40px;color:#ccc;font-weight: 900;filter: alpha(opacity:40);opacity: 0.4;}
#content a:hover{filter: alpha(opacity:90);opacity: 0.9;}
#prev{left:10px;}
#next{right:10px;}
#text,#span{position: absolute;bottom: 0;left:0;width:400px;height:30px;background:#000;line-height: 30px;text-align: center;color: #fff;filter: alpha(opacity:80);opacity: 0.8;}
#text{margin:0;bottom: 0;}
#span{top:0;}
#img{width:400px;height:400px;}
</style>
<script type="text/javascript">
   window.onload=function(){
   var oPrev=document.getElementById('prev');
   var oNext=document.getElementById('next');
   var oText=document.getElementById('text');
   var oSpan=document.getElementById('span');
   var oImg=document.getElementById('img');
   var oBtn1=document.getElementById('btn1');
   var oBtn2=document.getElementById('btn2');
   var oP1=document.getElementById('p1');
   var arrUrl=['images/1.jpg','images/2.jpg','images/3.jpg','images/4.jpg'];
   var arrText=['baby','冰冰','唐嫣','杨幂'];
   var num=0;
   //初始化
   function fnTab(){
   oSpan.innerHTML=num+1+'/'+ arrText.length;
   oText.innerHTML=arrText[num];
   oImg.src=arrUrl[num];
      };
      fnTab();
   oBtn1.onclick=function(){
    oNext.onclick=function(){
    num++;
    if(num==arrText.length){
     num=0;
    }
     fnTab();
   };
    oPrev.onclick=function(){
    num--;
    if(num==-1){
     num=arrText.length-1;
    }
     fnTab();
   };
    /*oPrev.onclick=function(){
     if(0<num){
      num--;
      fnTab();
     }else{
      num=arrText.length;
      num--;
     }
    };*/
   };
    oBtn2.onclick=function(){
    oP1.innerHTML = '图片只能到最后一张或第一张切换';
    oNext.onclick=function(){
    if(num==arrText.length-1){
     alert('这是最后一张了。。');
    }else{
     num++;
    }
    /*if(num==arrText.length){
     alert('这是最后一张了。。');
    }*/
     fnTab();
   };
    oPrev.onclick=function(){
    if(num==0){
    alert('这已经是第一张了,不能再切换了。。');
    }else{
     num--;
    }
     fnTab();
   };
   };
   /* oNext.onclick=function(){
    num++;
    if(num==arrText.length){
    num=0;
    }
    fnTab();
   };
   oPrev.onclick=function(){
    num--;
    if(num==-1){
    num=arrText.length-1;
    }
    fnTab();
   };*/
   };
</script>
</head>
<body>
  <div id="box">
   <div>
    <input id="btn1" type="button" value="循环切换">
    <input id="btn2" type="button" value="顺序切换">
   </div>
   <p id="p1">图片可以从最后一张跳到第一张循环切换</p>
  </div>
  <div id="content">
    <a href="javascript:;" rel="external nofollow" rel="external nofollow" id="prev"><</a>
    <a href="javascript:;" rel="external nofollow" rel="external nofollow" id="next">></a>
    <p id="text">图片文字加载中......</p>
    <span id="span">数量正在计算中......</span>
    <img id="img" />
  </div>
</body>
</html>

这是我使用js做的图片切换,可以选择顺序切换也可以选择循环切换顺序切换到最后一张会有提示。

本机测试运行效果如下:

希望本文所述对大家JavaScript程序设计有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们