本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《JavaScript图片操作技巧大全》、《JavaScript切换特效与技巧总结》、《JavaScript运动效果与技巧汇总》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]