您的位置:首页 > Web前端 > JavaScript

JS实现图片轮换

2010-03-24 15:06 190 查看
源代码

<!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" lang="gb2312">
<head>
<title>JS图片切换 ::</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="generator" content="editplus" />
<meta name="author" content="eLore" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<style type="text/css">
*{margin:0; padding:0; border:0;}
img{margin:0; padding:0; border:0;}

#top{
position:absolute;
top:10px; left:10px;
width:270px;
height:185px;
z-index:10000;
overflow:hidden;
}

#top .page{
position:relative;
top:160px;left:0px;
height:25px;
line-height:25px;
background:#000;
filter:alpha(opacity=78);
padding-left:8px;
color:#fff;
}

#top .page a{
font-size:14px;
font-family:宋体;
color:#f00;
font-weight:bold;
text-decoration:none;
}

#top .page a:hover{
font-size:14px;
font-family:宋体;
color:#ccc;
font-weight:bold;
text-decoration:none;
}
#top .page span{
float:right;
}

#top .page span a{
font-size:12px;
font-family:宋体;
font-weight:normal;
color:#fff;
text-decoration:none;
padding-right:10px;
}

#top .page span a:hover{
font-size:12px;
font-family:宋体;
font-weight:normal;
color:#f00;
text-decoration:none;
}

#js_F {
position:relative;
top:10px; left:10px;
overflow:hidden;
width:270px; height:185px;
background:#33c;
}
#js_F img{
position:absolute;
top:0; left:0;
width:270px; height:185px;
}

#js_F111 {
position:relative;
top:10px; left:10px;
overflow:hidden;
width:270px; height:185px;
background:#33c;
}
</style>
<script type="text/javascript">
<!--//
var js_F;
var imgList;
var imgTemp = new Array();
var imgs = new Array();
var imgID = 0, nextImgID, proveImgID;
var tf = true; //图片移动方向标志
var speed1 = 10; speed2 = 3000; //速度

var t1,t2;//时间循环

function imgInit(){
js_F = document.getElementById("js_F");
imgList = ["img/1.jpg", "img/2.jpg", "img/3.jpg", "img/4.jpg", "img/5.jpg"];

for (i=0; i<imgList.length; i++){
imgTemp[i] = new Image();
imgTemp[i].src = imgList[i];
}
var content = '';
for (i=0; i<imgList.length; i++){
content += '<a href=/'javascript:alert();/'><img src="' + imgList[i] + '" style="left:0;" /></a>/n';
}
js_F.innerHTML = content;
imgs = js_F.getElementsByTagName('img');
imgs[0].style.zIndex = 20;
imgs[1].style.zIndex = 15;
}

function imgChange(){
if ((imgID+1)<imgList.length){
nextImgID = imgID + 1;
} else if (imgID<imgList.length) {
nextImgID = 0;
} else {
imgID = 0;
nextImgID = imgID + 1;
}

for (i=0; i<imgList.length; i++){
if(i == imgID){
document.getElementById("a_" + i).style.color = "red";
document.getElementById("at_" + i).style.display = "";
}else{
document.getElementById("a_" + i).style.color = "white";
document.getElementById("at_" + i).style.display = "none";
}
if(i != imgID && i != nextImgID){
imgs[i].style.zIndex = 0;
}
}

imgs[imgID].style.zIndex = 20;
imgs[nextImgID].style.zIndex = 15;
startTimeout(1,speed2);//开始时间循环
}

function imgShow(){
if (tf){ //imgID向左边移动2/3,nextImgID向右边1/3
if (parseInt(imgs[imgID].style.left)>-180){
imgs[imgID].style.left = (parseInt(imgs[imgID].style.left)-10) + 'px';
imgs[nextImgID].style.left = (parseInt(imgs[nextImgID].style.left)+5) + 'px';
startTimeout(2,speed1);
//setTimeout('imgShow()',speed1);
} else {
tf = !tf;
imgs[imgID].style.zIndex = 15;
imgs[nextImgID].style.zIndex = 20;
startTimeout(2,speed1);
//setTimeout('imgShow()',speed1);
}
} else {
if (parseInt(imgs[imgID].style.left)<0){
imgs[imgID].style.left = (parseInt(imgs[imgID].style.left)+10) + 'px';
imgs[nextImgID].style.left = (parseInt(imgs[nextImgID].style.left)-5) + 'px';
startTimeout(2,speed1);
//setTimeout('imgShow()',speed1);
} else {
imgs[imgID].style.zIndex = 10;
tf = !tf;
imgID++;
imgChange();
}
}
}

function startTimeout(t,speed){
if(t == 1){
t1 = setTimeout('imgShow()',speed);
}
if(t == 2){
t2 = setTimeout('imgShow()',speed);
}
}

function goImg(Id){
clearTimeout(t1);
clearTimeout(t2);
imgID = Id;
imgChange();
}
//-->
</script>
</head>
<body onload="imgInit();imgChange();">
<div id="top">
<div class="page">
<span><a id="a_0" href="javascript:;" onclick="goImg(0)">1</a><a id="a_1" href="javascript:;" onclick="goImg(1)">2</a><a id="a_2" href="javascript:;" onclick="goImg(2)">3</a><a id="a_3" href="javascript:;" onclick="goImg(3)">4</a><a id="a_4" href="javascript:;" onclick="goImg(4)">5</a></span>
<a id="at_0" style="display:none">士大夫海枯石烂111</a>
<a id="at_1" style="display:none">士大夫海枯石烂222</a>
<a id="at_2" style="display:none">士大夫海枯石烂333</a>
<a id="at_3" style="display:none">士大夫海枯石烂444</a>
<a id="at_4" style="display:none">士大夫海枯石烂555</a>
</div>
</div>

<div id="js_F"></div>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: