DIV CSS 佈局教程網

用 jQuery 做表格上下翻頁:原理與輪播圖如出一轍
編輯:JQuery入門技巧     

彈窗裡的表格如果列數超過視窗高度,就需要翻頁。這個實作的思路和圖片輪播幾乎一樣——用 animate() 移動 tbody 的 top 值,讓不同的區段進入可視範圍。

注意:實現原理與輪播圖相似,只是方向改為垂直。

HTML

<div class="popup day02-popup04">
  <div class="group-caption">
    <span>日期</span>
    <span>參與團購場次</span>
    <span class="result">團購結果</span>
    <span>當前狀態</span>
  </div>

  <table class="group-buying-table J_group_buying_table">
    <tr><td>02.08</td><td>第一場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.09</td><td>第二場</td><td>失敗</td><td>G幣已退回</td></tr>
    <tr><td>02.10</td><td>第三場</td><td>團購中</td><td>團購中</td></tr>
    <tr><td>02.11</td><td>第一場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.12</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.13</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.14</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.15</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.16</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.17</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.18</td><td>第二場</td><td>成功</td><td>G幣已退回</td></tr>
    <tr><td>02.19</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.20</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.21</td><td>第二場</td><td>成功</td><td>團購中</td></tr>
    <tr><td>02.22</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.23</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
    <tr><td>02.24</td><td>第二場</td><td>成功</td><td>G幣已退回</td></tr>
    <tr><td>02.25</td><td>第二場</td><td>成功</td><td>現金券已發放</td></tr>
  </table>

  <p class="popup-page-btn">
    <a href="javascript:;" class="prev">上一頁</a>
    <a href="javascript:;" class="next">下一頁</a>
  </p>
  <a href="javascript:;" class="popup-close J_close"></a>
</div>

CSS

.day02-popup04 { width: 708px; height: 404px; }

.day02-popup04 .group-caption {
    width: 594px;
    margin: 30px auto 0;
    border-top: 1px solid #ccc;
    border-left: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
}

.day02-popup04 .group-caption span {
    width: 147.5px;
    display: inline-block;
    border-right: 1px solid #ccc;
    text-align: center;
    height: 50px;
    line-height: 50px;
    font-weight: 600;
    font-size: 20px;
}

.day02-popup04 .group-buying-table {
    position: relative;
    width: 594px;
    margin: 0 auto;
    height: 255px;
    overflow: hidden;
    border-collapse: collapse;
}

.day02-popup04 .group-buying-table tbody {
    position: absolute;
    top: 0;
}

.day02-popup04 .group-buying-table tbody tr {
    height: 50px;
    line-height: 50px;
}

.day02-popup04 .group-buying-table tbody tr td {
    width: 147px;
    border-left: 1px solid #ccc;
    border-right: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    text-align: center;
    font-size: 18px;
    color: #666;
}

.day02-popup04 .popup-page-btn {
    position: absolute;
    width: 100%;
    bottom: 0;
    height: 66px;
    line-height: 66px;
}

.day02-popup04 .popup-page-btn a {
    display: inline-block;
    text-align: center;
    width: 142px;
    margin: 0 12px;
    height: 42px;
    line-height: 42px;
    font-size: 20px;
    color: #fff;
    background-color: #bf3737;
}

JS 程式碼

var $groupTable = $('.J_group_buying_table');

var i = 5,                                  // 每頁顯示列數
    len = $groupTable.find('tbody tr').length, // 總列數
    page = 1,                               // 起始頁
    maxPage = Math.ceil(len / i),           // 總頁數
    $tbody = $groupTable.find('tbody'),
    $scrollHeight = $groupTable.height();   // 每次滾動距離

// 下一頁
$(".next").click(function (e) {
    if (!$tbody.is(":animated")) {
        if (page == maxPage) {
            $tbody.stop();
        } else {
            $tbody.animate({ top: "-=" + $scrollHeight + "px" }, 800);
            page++;
        }
    }
});

// 上一頁
$(".prev").click(function () {
    if (!$tbody.is(":animated")) {
        if (page == 1) {
            $tbody.stop();
        } else {
            $tbody.animate({ top: "+=" + $scrollHeight + "px" }, 800);
            page--;
        }
    }
});

為什麼要檢查 :animated

if (!$tbody.is(":animated")) 是這段程式最關鍵的一行。動畫跑 800 毫秒,如果使用者在這段期間連續點擊,若不加攔阻就會同時排入多個動畫,tbody 的位置會亂掉、頁碼也對不上。加上這個判斷等於「動畫進行中忽略點擊」,是最簡單有效的防護。

另一個隱藏的問題是 $scrollHeight = $groupTable.height()——它取的是表格(也就是可視視窗)的高度 255px。而每列高 50px、每頁 5 列,5 × 50 = 250px。兩者差 5px,翻頁時會有一點點累積的偏移。正確值應該是 $tbody.find('tr').first().outerHeight() * i,讓滾動距離真正等於一頁的內容高度。

最後,tbody 設為 position: absolute 才能用 top 做位移。這裡有個瀏覽器相容性的注意事項:部分舊版瀏覽器對 tbody 這種表格元素的定位支援不佳,比較保險的做法是在 table 外再包一層 div 並對它做位移。

與其手刻,也可以考慮直接用原生捲動:讓容器 overflow-y: auto,翻頁時呼叫 scrollBy({ top: height, behavior: 'smooth' })。程式碼更少,而且天然支援觸控手勢與滾輪,是現代專案的首選。

以上就是這個表格翻頁效果的完整說明,希望對你的開發有所幫助。

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