1. 程式人生 > >JavaScript 圖片切換

JavaScript 圖片切換

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文件</title>
<style>
p { margin:0; }
body { text-align:center; }
#content { width:400px; height:400px; border:10px solid #ccc; margin:50px auto 0; position:relative; }
#content a { width: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; }
#content a:hover { filter:alpha(opacity:30); opacity:0.3; }
#prev { left:10px; }
#next { right:10px; }
#p1 { width: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; }
strong { width: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; }
#img1 { width:400px; height:400px; }
span { position:absolute; width:400px; height:30px; line-height:30px; text-align:center; top:-50px; left:0; font-family:'微軟雅黑'; }
</style>
<script>
window.onload = function (){
	var oPrev = document.getElementById('prev');
	var oNext = document.getElementById('next');
	var oP = document.getElementById('p1');
	var oSpan = document.getElementById('span1');
	var oImg = document.getElementById('img1');
	var aBtn = document.getElementsByTagName('input');
	
	var arrUrl = [ 'img/1.jpg', 'img/2.jpg', 'img/3.jpg', 'img/4.jpg' ];
	var arrText = [ '文字一', '文字二', '文字三', '識文斷字' ];
	
	var num=0;
	function fnTab(){
		//初始化    1/4  字串連線
		oSpan.innerHTML=num+1+'/'+arrText.length;
		//初始化 插入照片
		oImg.src=arrUrl[num]
		//初始化 文字
		oP.innerHTML=arrText[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>

<input type="button" value="迴圈切換" />
<input type="button" value="順序切換" />


<div id="content">
	<a id="prev" href="javascript:;"><</a>
	<a id="next" href="javascript:;">></a>
    <p id="p1">圖片文字載入中……</p>
    <span id="span1">圖片數量計算中……</span>
	<img id="img1" />
</div>

</body>
</html>