Javascript 自動播放投影片效果代碼

來源:互聯網
上載者:User
 代碼如下 複製代碼

<!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>
body,div,ul,li{margin:0;padding:0;}
ul{list-style-type:none;}
body{background:#000;text-align:center;font:12px/20px Arial;}
#box{position:relative;width:492px;height:172px;background:#fff;border-radius:5px;border:8px solid #fff;margin:10px auto;cursor:pointer;}
#box .list{position:relative;width:490px;height:170px;overflow:hidden;}
#box .list ul{position:absolute;top:0;left:0;}
#box .list li{width:490px;height:170px;overflow:hidden;}
#box .count{position:absolute;right:0;bottom:5px;}
#box .count li{color:#fff;float:left;width:20px;height:20px;cursor:pointer;margin-right:5px;overflow:hidden;background:#F90;opacity:0.7;filter:alpha(opacity=70);border-radius:20px;}
#box .count li.current{color:#fff;opacity:1;filter:alpha(opacity=100);font-weight:700;background:#f60;}
#tmp{width:100px;height:100px;background:red;position:absolute;}
</style>
<script type="text/javascript">
window.onload = function ()
{
 var oBox = document.getElementById("box");
 var oList = oBox.getElementsByTagName("ul")[0];
 var aImg = oBox.getElementsByTagName("img");
 var timer = playTimer = null;
 var index = i = 0;
 var bOrder = true;
 var aTmp = [];
 var aBtn = null;
 
 //產生數字按鈕
 for (i = 0; i < aImg.length; i++) aTmp.push("<li>" + (i + 1) + "</li>");
 
 //插入元素
 var oCount = document.createElement("ul");
 oCount.className = "count";
 oCount.innerHTML = aTmp.join("");
 oBox.appendChild(oCount); 
 aBtn = oBox.getElementsByTagName("ul")[1].getElementsByTagName("li");
 
 //初始化狀態
 cutover();
 
 //按鈕點擊切換
 for (i = 0; i < aBtn.length; i++)
 {
  aBtn[i].index = i;
  aBtn[i].onmouseover = function ()
  {
   index = this.index;
   cutover()
  }
 }
 
 function cutover()
 {
  for (i = 0; i < aBtn.length; i++) aBtn[i].className = "";
  aBtn[index].className = "current";   
  startMove(-(index * aImg[0].offsetHeight))
 }
 
 function next()
 {
  bOrder ? index++ : index--;
  index <= 0 && (index = 0, bOrder = true);
  index >= aBtn.length - 1 && (index = aBtn.length - 1, bOrder = false)
  cutover()
 }
 
 playTimer = setInterval(next, 3000);
 
 //滑鼠移入展示區停止自動播放
 oBox.onmouseover = function ()
 {
  clearInterval(playTimer)
 };
 
 //滑鼠離開展示區開始自動播放
 oBox.onmouseout = function ()
 {
  playTimer = setInterval(next, 3000)
 };
 function startMove(iTarget)
 {
  clearInterval(timer);
  timer = setInterval(function ()
  {
   doMove(iTarget)
  }, 30) 
 }
 function doMove (iTarget)
 {  
  var iSpeed = (iTarget - oList.offsetTop) / 10;
  iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);  
  oList.offsetTop == iTarget ? clearInterval(timer) : oList.style.top = oList.offsetTop + iSpeed + "px"
 }
};
</script>
</head>
<body>
<div id="box">
    <div class="list">
        <ul>
            <li><img src="img/01.jpg" width="490" height="170" /></li>
            <li><img src="img/02.jpg" width="490" height="170" /></li>
            <li><img src="img/03.jpg" width="490" height="170" /></li>
            <li><img src="img/04.jpg" width="490" height="170" /></li>
            <li><img src="img/05.jpg" width="490" height="170" /></li>
        </ul>
    </div>
</div>
</body>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.