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+'https://www.jb51.net/'+ 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> <div> <input type="button" value="循环切换"> <input type="button" value="顺序切换"> </div> <p>图片可以从最后一张跳到第一张循环切换</p> </div> <div> <a href="javascript:;"><</a> <a href="javascript:;">></a> <p>图片文字加载中......</p> <span>数量正在计算中......</span> <img /> </div> </body> </html>

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

本机测试运行效果如下:

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

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript图片操作技巧大全》、《JavaScript切换特效与技巧总结》、《JavaScript运动效果与技巧汇总》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/16a68d86eb1cb9092c2dbec5e0c2b33b.html