DIV CSS 佈局教程網

用 jQuery 封裝可重複使用的 Tab 自動切換效果
編輯:JQuery特效代碼     

做專案時經常會碰到 Tab 自動輪播切換的需求,因此寫成一篇筆記做個總結,也方便日後參考。以下示範如何用 jQuery 封裝成一個可在同一頁面多次呼叫的 tabSlider 類別。

呼叫方式為 new tabSlider(obj, count),其中 obj 是最外層容器的 ID,count 是 li 的數量。要使用這個 js,HTML 結構必須依約定撰寫:

<div id="tab1">
    <ul class="menu">
        <li class="current">tab1</li>
        <li>tab2</li>
        <li>tab3</li>
        <li class="last-col">tab4</li>
    </ul>
    <div class="content-main">
        <div class="content">tab1</div>
        <div class="content hide">tab2</div>
        <div class="content hide">tab3</div>
        <div class="content hide">tab4</div>
    </div>
</div>

頁面 HTML / CSS

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<style>
#tab1{ width:196px; height:220px; overflow:hidden; border:1px solid #666;}
#tab2{ width:196px; height:220px; overflow:hidden; border:1px solid #666;}
.menu{ width:196px; height:18px; margin:0; padding:0;}
.menu li{ float:left; list-style:none; width:48px; text-align:center; border-bottom:1px solid #666; border-right:1px solid #666; cursor:pointer;}
.menu li.current{ border-bottom:none;}
.content-main{ width:196px; height:200px; overflow:hidden;}
.content{ width:196px; height:200px; overflow:hidden; display:block;}
.hide{ display:none;width:196px; height:200px; overflow:hidden;}
.menu li.last-col{ border-right:none; width:49px;}
</style>
<script src="AutoTab.js"></script>

同一頁可放多組,各自呼叫即可:

<script>new tabSlider("#tab1",4);</script>
<script>new tabSlider("#tab2",4);</script>

封裝的 JS

function tabSlider(obj,count){
    var _this = this;
    _this.obj = obj;
    _this.count = count;
    _this.time = 2000; // 停留的時間
    _this.n = 0;
    var  t;
    this.slider = function(){
        $(_this.obj + " .menu li").bind("mouseover",function(event){
            $(event.target).addClass("current").siblings().removeClass("current");
            var index = $(_this.obj + " .menu li").index(this);
            $(_this.obj + " .content-main .content").eq(index).show().siblings().hide();
            _this.n = index;
        })
    }
    this.addhover = function(){
        $(_this.obj).hover(function(){clearInterval(t);},function(){t=setInterval(_this.autoplay,_this.time)});
    }
    this.autoplay = function(){
        _this.n = _this.n >=(_this.count-1)? 0 : ++_this.n;
        $(_this.obj + " .menu li").eq(_this.n).trigger("mouseover");
    }
    this.factory = function(){
        this.slider();
        this.addhover();
        t = setInterval(this.autoplay,_this.time);
    }
    this.factory();
}

封裝思路

寫任何程式,思路最重要,想得清楚程式碼自然寫得出來。這裡的做法是:

  1. 用 tabSlider(obj, count) 封裝並接收參數,呼叫時直接 new 即可。
  2. 初始化參數,自訂 slider() 綁定 mouseover,讓滑過的 li 對應的內容顯示、其餘隱藏。
  3. 自訂 addhover(),滑鼠移入時暫停、移出時恢復自動播放。
  4. 自訂 autoplay() 做自動輪播,用 trigger("mouseover") 觸發切換。
  5. 最後用 factory() 依序呼叫上述方法完成初始化。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved