jQuery封裝的tab組件(可選自動+可選延遲處理+按需載入)

来源:https://www.cnblogs.com/chenyingying0/archive/2020/02/29/12384972.html
-Advertisement-
Play Games

效果圖 tab2.html <!DOCTYPE html> <html lang="zh-CN"><!-- 設置簡體中文 --> <head> <meta charset="UTF-8"> <title>tab2按需載入</title> <link rel="stylesheet" href=".. ...


效果圖

tab2.html

<!DOCTYPE html>
<html lang="zh-CN"><!-- 設置簡體中文 -->
<head>
    <meta charset="UTF-8">
    <title>tab2按需載入</title>
    <link rel="stylesheet" href="../css/base.css">
    <link rel="stylesheet" href="../css/tab2.css">
    <!-- css一般放在DOM載入前,防止DOM裸奔 -->
</head>
<body>
    <!-- 樓層一 -->
    <div class="floor">
        <div class="container">
            <div class="tab-head">
                <div class="tab-head-title fl">
                    <span class="tab-head-title-num">1F</span><span class="tab-head-title-name">服裝箱包</span>
                </div>
                <ul class="tab-head-item-wrap fr">
                    <li class="fl"><a href="javascript:;" class="tab-head-item tab-head-item-active">大牌</a></li>
                    <li class="fl tab-head-item-divider text-hidden">分割線</li>
                    <li class="fl"><a href="javascript:;" class="tab-head-item">男裝</a></li>
                    <li class="fl tab-head-item-divider text-hidden">分割線</li>
                    <li class="fl"><a href="javascript:;" class="tab-head-item">女裝</a></li>
                </ul>
            </div>
            <div class="tab-body">
                <ul class="tab-body-panel">
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/1/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/1/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/1/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/1/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/1/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/1/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                </ul>
                <ul class="tab-body-panel">
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/2/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/2/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/2/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/2/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/2/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/2/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                </ul>
                <ul class="tab-body-panel">
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/3/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/3/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/3/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/3/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/3/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                    <li class="tab-body-panel-item fl">
                        <p class="tab-body-panel-item-pic"><a href="#" class="link"><img src="../img/floor/loading.gif" data-src="../img/floor/1/3/1.png" class="floor-img"></a></p>
                        <p class="tab-body-panel-item-name">匡威男棒球開衫外套2020</p>
                        <p class="tab-body-panel-item-price">¥479</p>
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <script src="../js/jquery.js"></script>
    <script src="../js/transition.js"></script>
    <script src="../js/showhide.js"></script>
    <script src="../js/tab2.js"></script>
</body>
</html>

base.css https://www.cnblogs.com/chenyingying0/p/12363689.html

tab2.css 

/*common*/
    .container{
        width:1200px;
        margin:0 auto;
    }
    .link{
        color:#4d555d;
    }
    a.link:hover{
        color:#f01414;
    }
    .fl{
        float:left;
    }
    /*文字隱藏*/
    .text-hidden{
        text-indent:-9999px;
        overflow:hidden;
    }
/*showhide*/
    .fadeOut{
        opacity: 0;
        visibility: hidden;
    }
    .slideUpDownCollapse{
        height:0 !important;/*避免因為優先順序不夠而無法生效*/
        padding-top:0 !important;
        padding-bottom:0 !important;
    }
    .slideLeftRightCollapse{
        width:0 !important;/*避免因為優先順序不夠而無法生效*/
        padding-left:0 !important;
        padding-right:0 !important;
    }

/*floor樓層區域*/
    .floor{
        margin-top:7px;
    }
    .tab-head{
        height:68px;
        border-bottom:1px solid #f01414;
    }
    .tab-head-title{
        margin-top:22px;
    }
    .tab-head-title-num{
        display: inline-block;
        width:24px;
        height:24px;
        border-radius:50%;
        background-color:#07111b;
        color:#fff;
        text-align: center;
        line-height:24px;
        margin-right:10px;
    }
    .tab-head-title-name{
        font-size:20px;
        color:#07111b;
        position: relative;
        top:3px;
    }
    .tab-head-item-wrap{
        margin-top:8px;
        height:62px;
        line-height:62px;
    }
    .tab-head-item{
        display: inline-block;
        font-size:14px;    
        color:#93999f;
        margin-right:16px;
    }
    .tab-head-item-active{
        color:#f01414;
        background:url(../img/floor-arrow.png) center bottom no-repeat;
    }
    .tab-head-item-divider{
        display: inline-block;
        width:1px;
        height:14px;
        background-color:#d9dde1;
        margin-top:21px;
        margin-right:16px;
    }
    .tab-body{
        height:233px;
    }
    .tab-body-panel{
        height:466px;
        display: none;
    }
    .tab-body-panel-item{
        width:200px;
        height:233px;
        text-align: center;
    }
    .tab-body-panel-item:hover{
        box-shadow:0 0 10px rgba(0,0,0,.2);
    }
    .tab-body-panel-item-pic{
        margin-top:24px;
    }
    .tab-body-panel-item-name{
        margin-top:23px;
        font-size:12px;
        color:#07111b;
    }
    .tab-body-panel-item-price{
        margin-top:9px;
        font-size:14px;
        color:#f01414;
    }

transition.js https://www.cnblogs.com/chenyingying0/p/12363649.html

showhide.js https://www.cnblogs.com/chenyingying0/p/12363707.html

tab2.js

(function($){
    "use strict";

    function Tab(elem,options){
        this.elem=elem;
        this.options=options;

        this.items=this.elem.find(".tab-head-item");//tab選項卡
        this.panels=this.elem.find(".tab-body-panel");//tab選項面板
        this.tabNum=this.items.length;//tab選項數量
        this.curIdx=this._getIdx(this.options.activeIdx);//當前選項卡索引

        this._init();//初始化
    }

    //預設參數
    Tab.defaults={
        event:"mouseenter",//click
        css3:false,
        js:false,
        animation:"fade",
        activeIdx:0,
        interval:0,
        delay:0//是否延遲
    };

    Tab.prototype._init=function(){
        var self=this;
        var timer=null;

        //init show
        this.items.removeClass("tab-head-item-active");
        this.items.eq(this.curIdx).addClass("tab-head-item-active");//指定item添加樣式
        this.panels.eq(this.curIdx).show();//指定panel顯示

        //trigger event
        this.panels.on("show shown hide hidden",function(e){
            self.elem.trigger("tab-"+e.type,[e.type,self.panels.index(this),this]);
            //傳遞的參數:事件類型 觸發事件的索引,觸發事件的元素
        })

        //showHide init
        this.panels.showHide(this.options);

        //bind event
        this.options.event=this.options.event==="click"?"click":"mouseenter";
        //事件代理,替被點擊的tab項做代理
        this.elem.on(this.options.event,".tab-head-item",function(){
            var elem=this;//elem指向被點擊的那個.tab-head-item

            if(self.options.delay){	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • [Android應用程式與SurfaceFlinger服務的連接過程分析 ](https://blog.csdn.net/luoshengyang/article/details/7857163) Android系統的開機動畫是由應用程式bootanimation來實現的,它位於/system/bi ...
  • 註意:無特殊說明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 基礎用法 PageView控制項可以實現一個“圖片輪播”的效果,PageView不僅可以水平滑動也可以垂直滑動,簡單用法如下: PageView滾動方向預設是水 ...
  • 瀏覽器通過form表單把數據提交伺服器涉及http協議,模式為瀏覽器發送請求=>伺服器接收=>自身處理=>結果返回瀏覽器=>瀏覽器根據結果展示頁面給用戶,請求分為GET和POST。註意:所有表單控制項(文本框、文本域、按鈕、單選框、覆選框等)都必須放在表單中,其次同一組的單選按鈕,name 取值一定要... ...
  • 在前端開發中,表格主要用於存儲結構化的數據,CSS主要用於修飾DOM元素,如何通過DOM來操作表格及CSS呢?本文主要通過一些簡單的小例子,簡述DOM對錶格和CSS的常見操作方法,僅供學習分享使用,如有不足之處,還請指正。 ...
  • Set 數據結構 ES6 提供了新的數據結構 Set,註意不是數據類型,而是數據結構。它類似於數組,但是成員的值(可以是任何類型,無論是原始值或者是對象引用都行)都是唯一的,沒有重覆的值。 Set 中的特殊值 Set 數據結構存儲的成員 值總是唯一的,所以Set數據結構在添加新成員時需要判斷兩個值是 ...
  • 一、2D效果屬性 要使用這些屬性,我們需要通過transform,並且,為了保證相容性,我們可能還需要添加帶有瀏覽器內核首碼的屬性,比如 -webkit-transform。 在認識屬性前先知道這裡的使用坐標系是怎樣的,其坐標系與傳統的數學坐標系不同。 坐標系簡單分析如下: 1.預設狀態下,x軸是水 ...
  • 1.塊級作用域 什麼是: 在一個代碼塊(括在一對花括弧中的一組語句)中定義的所需變數(與let配合使用)併在代碼塊的外部是不可見的。 為什麼: 在ES6之前,函數只能在全局作用域和函數作用域中聲明,不能在塊級作用域中聲明,造成諸多問題: 1.內層變數會覆蓋外層變數 2.用來計數的迴圈變數泄漏變成全局 ...
  • 偽元素 偽元素可以創建一些文檔語言無法創建的虛擬元素。比如:文檔語言沒有一種機制可以描述元素內容的第一個字母或第一行,但偽元素可以做到(::first-letter、::first-line)。同時,偽元素還可以創建源文檔不存在的內容,比如使用 ::before 或 ::after。 不能用偽元素插 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...