DIV CSS 佈局教程網

同一頁面放多組焦點圖:可重複呼叫的 JS 輪播寫法
編輯:關於JavaScript     

做電商或入口網站首頁時,常會需要在同一頁擺好幾組輪播。如果每組都複製一份程式碼,維護起來非常痛苦。下面這個寫法把輪播封裝成建構函式,只要 new 幾次就能產生幾組,各自還能帶不同參數。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>js 同一頁面可多次調用的圖片幻燈切換效果</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<script language="javascript">
// 聲明命名空間
var $o = new Object();

// 基礎方法
$o.base = {
  e: function(a, f){
    for(var i = 0, j = a.length; i < j; i++){ f.call(a[i], i); }
  }
}

$o.slide = function (id, arg){
  var arg = arg || {},
      t = document.getElementById(id),
      a = t.getElementsByTagName("a"),
      lis = [],
      cl = arg.color || '#f30',
      ctm = arg.time * 1000 || 2000,
      w = t.clientWidth,
      h = t.clientHeight,
      b = ['<ul style="margin:0; padding:0; list-style:none; display:block; position:absolute; bottom:10px; right:10px;">'],
      index = 0, $ = null;

  function change(i){
    if(!!$) { clearTimeout($); }
    index = !isNaN(i) ? i : index + 1;
    if(index >= a.length) { index = 0; }
    $o.base.e(lis, function(k){
      if(k == index) { c(1, this); } else { c(0, this); }
    });
    var to = - index * h;
    if(a[0].offsetTop == to) { return; }
    else {
      if(!!$) { clearInterval($); }
      $ = setInterval(function(){
        var ot = a[0].offsetTop;
        v = Math[to < ot ? 'floor' : 'ceil']((to - ot) * 0.2);
        if(ot == to) { clearInterval($); $ = null; st(); }
        ot += v;
        a[0].style.marginTop = ot + "px";
      }, 30);
    }
  }

  function c(b, o){
    o.style.backgroundColor = !!b ? cl : "#fff";
    o.style.color = !!b ? "#fff" : cl;
  }

  function st(){
    if(!!$) clearTimeout($);
    $ = setTimeout(function(){ change() }, ctm);
  }

  with(t.style){ overflow = 'hidden'; position = 'relative'; }

  $o.base.e(a, function(n){
    this.style.display = "block";
    with(this.firstChild.style){
      borderWidth = '0';
      width = w + 'px';
      height = h + 'px';
    }
    b.push('<li style="width:20px; height:20px; line-height:20px; display:block; float:left; margin-left:5px; border:1px solid ' + cl + '; background-color:#fff; color:' + cl + '; font-size:14px; text-align:center; cursor:pointer;">' + (n + 1) + '</li>');
  });
  b.push('</ul>');
  t.innerHTML += b.join("");
  lis = t.getElementsByTagName("li");
  $o.base.e(lis, function(n){
    if(n == index) { c(1, this); }
    this.onmouseover = function(){ if(n != index) change(n); };
  });
  st();
}
</script>
</head>
<body>

<div id="aa" style="width:402px;height:228px;">
  <a href="javascript:alert('1')"><img src="/images/1.jpg"></a>
  <a href="javascript:alert('2')"><img src="/images/2.jpg"></a>
  <a href="javascript:alert('3')"><img src="/images/3.jpg"></a>
  <a href="javascript:alert('4')"><img src="/images/4.jpg"></a>
  <a href="javascript:alert('5')"><img src="/images/5.jpg"></a>
  <a href="javascript:alert('6')"><img src="/images/6.jpg"></a>
  <a href="javascript:alert('7')"><img src="/images/7.jpg"></a>
  <a href="javascript:alert('8')"><img src="/images/8.jpg"></a>
  <a href="javascript:alert('9')"><img src="/images/9.jpg"></a>
</div>
<script language="javascript">new $o.slide("aa");</script>

<br>
<div id="bb" style="width:402px;height:228px;">
  <a href="###"><img src="/images/1.jpg"></a>
  <a href="###"><img src="/images/2.jpg"></a>
  <a href="###"><img src="/images/3.jpg"></a>
  <a href="###"><img src="/images/4.jpg"></a>
  <a href="###"><img src="/images/5.jpg"></a>
</div>
<script language="javascript">new $o.slide("bb", {color:'#000', time:0.2});</script>

<br>
<div id="cc" style="width:402px;height:228px;">
  <a href="###"><img src="/images/1.jpg"></a>
  <a href="###"><img src="/images/2.jpg"></a>
  <a href="###"><img src="/images/3.jpg"></a>
  <a href="###"><img src="/images/4.jpg"></a>
  <a href="###"><img src="/images/5.jpg"></a>
</div>
<script language="javascript">new $o.slide("cc", {color:'green'});</script>

</body>
</html>

設計重點

  • 命名空間隔離:用 $o 物件包住方法,避免污染全域;$o.base.e 是通用的陣列/集合迭代器。
  • 參數可調:第二個參數支援 color(指示器顏色)與 time(自動輪播秒數),不傳則走預設值。
  • 指示器自動生成:依 <a> 的數量動態拼出 <ul><li>,不必手寫頁碼。
  • 緩動位移:用 setInterval 每次移動差距的 0.2 倍,形成先快後慢的淡出式捲動。
  • 狀態互相獨立:三組實例各自維護自己的 index 與計時器,彼此不干擾。

這種「封裝成建構函式+參數物件」的思路,其實就是元件化的雛形。用現代寫法重寫時,把 $o.slide 換成 class、把 setInterval 換成 requestAnimationFrame 或 CSS transition,行為會更順,但整體結構是一樣的。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved