JS實(shí)現(xiàn)的圖片選擇順序切換和循環(huán)切換功能示例【測(cè)試可用】
本文實(shí)例講述了JS實(shí)現(xiàn)的圖片選擇順序切換和循環(huán)切換功能。分享給大家供大家參考,具體如下:
<!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ù)量正在計(jì)算中......</span> <img id="img" /> </div> </body> </html>
這是我使用js做的圖片切換,可以選擇順序切換也可以選擇循環(huán)切換順序切換到最后一張會(huì)有提示。
本機(jī)測(cè)試運(yùn)行效果如下:
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript圖片操作技巧大全》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript運(yùn)動(dòng)效果與技巧匯總》、《JavaScript動(dòng)畫(huà)特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
解決layui-table單元格設(shè)置為百分比在ie8下不能自適應(yīng)的問(wèn)題
今天小編就為大家分享一篇解決layui-table單元格設(shè)置為百分比在ie8下不能自適應(yīng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09微信小程序前后端數(shù)據(jù)交互的詳細(xì)圖文教程
這篇文章主要給大家介紹了關(guān)于微信小程序前后端數(shù)據(jù)交互的相關(guān)資料,通過(guò)小程序向后端發(fā)送請(qǐng)求,然后后端從數(shù)據(jù)庫(kù)獲取車(chē)源和求購(gòu)的數(shù)量反饋給小程序,最后將這兩個(gè)數(shù)據(jù)顯示出來(lái),需要的朋友可以參考下2022-10-10javascript設(shè)計(jì)模式 – 工廠模式原理與應(yīng)用實(shí)例分析
這篇文章主要介紹了javascript設(shè)計(jì)模式 – 工廠模式,結(jié)合實(shí)例形式分析了javascript工廠模式基本概念、原理、定義、應(yīng)用場(chǎng)景及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-04-04