温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JS如何实现的图片选择顺序切换和循环切换功能

发布时间:2021-04-20 09:57:20 来源:亿速云 阅读:227 作者:小新 栏目:web开发

这篇文章给大家分享的是有关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如何实现的图片选择顺序切换和循环切换功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI