图片切换综合实例

 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 5 <title>无标题文档</title>
 6 <style>
 7 p { margin:0; }
 8 body { text-align:center; }
 9 #box { 400px; height:400px; border:10px solid #ccc; margin:50px auto 0; position:relative; }
10 a { 40px; height:40px; background:#fff; filter:alpha(opacity:80); opacity:0.8; position:absolute; top:160px; font-size:18px; color:#000; text-align:center; line-height:40px; text-decoration:none; }
11 a:hover { filter:alpha(opacity:30); opacity:0.3; }
12 #prev { left:10px; }
13 #next { right:10px; }
14 #p1 { 400px; height:30px; line-height:30px; text-align:center; background:#000; color:#fff; font-size:14px; filter:alpha(opacity:80); opacity:0.8; position:absolute; bottom:0; left:0; }
15 strong { 400px; height:30px; line-height:30px; text-align:center; background:#000; color:#fff; font-size:14px; filter:alpha(opacity:80); opacity:0.8; position:absolute; top:0; left:0; }
16 #img1 { 400px; height:400px; }
17 span { position:absolute; 400px; height:30px; line-height:30px; text-align:center; top:-50px; left:0; font-family:'微软雅黑'; }
18 </style>
19 <script>
20 window.onload = function (){
21     var oPrev = document.getElementById('prev');
22     var oNext = document.getElementById('next');
23     var oP = document.getElementById('p1');
24     var oStrong = document.getElementById('strong1');
25     var oImg = document.getElementById('img1');
26     var aBtn = document.getElementsByTagName('input');
27     
28     var arrUrl = [ 'img/1.jpg', 'img/2.jpg', 'img/3.jpg', 'img/4.jpg' ];
29     var arrText = [ '文字一', '文字二', '文字三', '识文断字' ];
30     var num = 0;
31     var onOff = true;
32     
33     aBtn[0].onclick = function (){ onOff = true; document.getElementsByTagName('span')[0].innerHTML = '图片可从最后一张跳转到第一张循环切换'; };
34     aBtn[1].onclick = function (){ onOff = false; document.getElementsByTagName('span')[0].innerHTML = '图片只能到最后一张或只能到第一张切换'; };
35     
36     // 初始化
37     function fnTab(){
38         oP.innerHTML = arrText[num];
39         oStrong.innerHTML = num+1 + ' / ' + arrText.length;
40         oImg.src = arrUrl[num];
41     }
42     fnTab();
43     
44     oPrev.onclick = function (){
45         num --;
46         if( num == -1 ){
47             if(onOff){num = arrText.length-1;}else{
48                 alert('这已经是第一张了,不能再往前了~~');
49                 num = 0;
50             }
51             //num = arrText.length-1;
52         }
53         fnTab();
54     };
55     oNext.onclick = function (){
56         num ++;
57         if( num == arrText.length ){
58             if(onOff){num = 0;}else{
59                 alert('已经到最后一张啦~');
60                 num = arrText.length-1;
61             }
62         }
63         fnTab();
64     };
65     
66 };
67 </script>
68 </head>
69 
70 <body>
71 
72 <input type="button" value="循环切换" />
73 <input type="button" value="顺序切换" />
74 
75 
76 <div id="box"><span>图片可从最后一张跳转到第一张循环切换</span>
77     <a id="prev" href="javascript:;"><</a>
78     <a id="next" href="javascript:;">></a>
79   <p id="p1">图片文字加载中……</p>
80   <strong id="strong1">图片数量计算中……</strong>
81     <img id="img1" />
82 </div>
83 
84 </body>
85 </html>
示例代码
原文地址:https://www.cnblogs.com/123wyy123wyy/p/6896618.html