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" 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 οnlοad="imgInit();imgChange();">
  <div id="top">
   <div class="page">
     <span><a id="a_0" href="javascript:;" οnclick="goImg(0)">1</a><a id="a_1" href="javascript:;" οnclick="goImg(1)">2</a><a id="a_2" href="javascript:;" οnclick="goImg(2)">3</a><a id="a_3" href="javascript:;" οnclick="goImg(3)">4</a><a id="a_4" href="javascript:;" οnclick="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>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值