萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML 表格進階特效實作:3D 邊框、漸層列、滑鼠互動與骨架屏載入

提升資料表視覺體驗的現代 CSS 技巧

資料表不再只是枯燥的格線。善用 CSS 3D 邊框、漸層背景、過渡動畫、偽元素,能打造專業級的視覺層次與互動回饋,強化品牌識別與使用者體驗。

經典 3D 立體邊框(Windows 95 / Material 風格)

.table-3d {
  border-collapse: separate;
  border-spacing: 0;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

.table-3d th,
.table-3d td {
  border: 2px solid;
  /* 上左亮、下右暗 = 凸起效果 */
  border-color: #ffffff #b0b0b0 #b0b0b0 #ffffff;
  padding: 12px 16px;
}

.table-3d thead th {
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-color: #ffffff #ced4da #ced4da #ffffff;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 0.8rem;
  color: #495057;
}

.table-3d tbody tr:nth-child(odd) td {
  background: #fdfdfd;
}
.table-3d tbody tr:nth-child(even) td {
  background: #f8f9fa;
}

/* 按壓效果 */
.table-3d tbody tr:active td {
  border-color: #b0b0b0 #ffffff #ffffff #b0b0b0; /* 反轉 = 按下 */
  background: #e9ecef;
}

漸層交錯列與滑鼠高亮欄列

.table-gradient {
  border-collapse: collapse;
  overflow: hidden; /* 圓角生效 */
  border-radius: 8px;
}

.table-gradient th {
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 14px 16px;
  font-weight: 500;
}

.table-gradient td {
  padding: 12px 16px;
  border-bottom: 1px solid rgba(0,0,0,0.05);
  background: linear-gradient(90deg, #ffffff 0%, #f8f9fa 100%);
  transition: background 0.2s ease;
}

.table-gradient tbody tr:hover td {
  background: linear-gradient(90deg, #eef2ff 0%, #e0e7ff 100%) !important;
}

/* 同時高亮該欄 */
.table-gradient td {
  position: relative;
}
.table-gradient td::after {
  content: '';
  position: absolute;
  top: 0; bottom: 0; left: 0; right: 0;
  background: rgba(102, 126, 234, 0.06);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}
.table-gradient td:hover::after { opacity: 1; }
.table-gradient tr:hover td::after { opacity: 0; } /* 列高亮優先 */

骨架屏載入動畫

.table-skeleton {
  border-collapse: collapse;
}
.table-skeleton th,
.table-skeleton td {
  padding: 12px 16px;
  border-bottom: 1px solid #e9ecef;
}
.table-skeleton .skeleton-cell {
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite linear;
  border-radius: 4px;
  height: 1rem;
}
.table-skeleton .skeleton-cell.short { width: 60%; }
.table-skeleton .skeleton-cell.medium { width: 80%; }
.table-skeleton .skeleton-cell.long { width: 100%; }

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* JS 切換真實資料 */
function showTableData(container, data) {
  container.innerHTML = renderTable(data); // 替換骨架屏
  container.classList.remove('table-skeleton');
  container.classList.add('table-gradient');
}

列印與無障礙優化

@media print {
  .table-3d, .table-gradient {
    box-shadow: none;
    border: 1px solid #000;
  }
  .table-3d th, .table-3d td,
  .table-gradient th, .table-gradient td {
    border: 1px solid #000 !important;
    background: white !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .table-gradient td::after { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .table-gradient td,
  .table-skeleton .skeleton-cell {
    transition: none;
    animation: none;
  }
}

/* 高對比模式 */
@media (prefers-contrast: high) {
  .table-3d th, .table-3d td {
    border-color: #000;
  }
}

響應式策略:水平捲軸 vs 卡片切換

.table-responsive {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}

@media (max-width: 640px) {
  .table-card-view thead { display: none; }
  .table-card-view tbody { display: block; }
  .table-card-view tr { 
    display: block; 
    border: 1px solid #dee2e6; 
    border-radius: 8px; 
    margin-bottom: 1rem; 
    padding: 1rem;
    background: white;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
  }
  .table-card-view td { 
    display: flex; 
    justify-content: space-between; 
    padding: 0.5rem 0; 
    border: none; 
    border-bottom: 1px solid #f1f3f4;
  }
  .table-card-view td:last-child { border-bottom: none; }
  .table-card-view td::before { 
    content: attr(data-label); 
    font-weight: 600; 
    color: #6c757d; 
  }
}

/* HTML 需加 data-label */
<td data-label="價格">$1,200</td>

框架整合快速啟用

  • Bootstrap 5: <table class="table table-hover table-striped shadow-sm">
  • Tailwind CSS: <table class="w-full divide-y divide-gray-200 hover:bg-blue-50">
  • 自訂設計系統:定義 --table-border, --table-header-bg, --table-row-hover CSS 變數

表格特效的核心原則:**服務於資料可讀性**,而非炫技。3D 邊框適合儀表板、漸層列適合報表、骨架屏提升感知速度、卡片模式解決行動端溢出。選對工具,資料表也能成為介面亮點。

copyright © 萬盛學電腦網 all rights reserved