溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JS如何實現(xiàn)的圖片選擇順序切換和循環(huán)切換功能

發(fā)布時間:2021-04-20 09:57:20 來源:億速云 閱讀:238 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)JS如何實現(xiàn)的圖片選擇順序切換和循環(huán)切換功能的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

具體如下:

<!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('這已經(jīng)是第一張了,不能再切換了。。');
    }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="循環(huán)切換">
    <input id="btn2" type="button" value="順序切換">
   </div>
   <p id="p1">圖片可以從最后一張?zhí)降谝粡堁h(huán)切換</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">數(shù)量正在計算中......</span>
    <img id="img" />
  </div>
</body>
</html>

感謝各位的閱讀!關(guān)于“JS如何實現(xiàn)的圖片選擇順序切換和循環(huán)切換功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI