如何在可捲動Div 中建立固定標題表
常見的Web 開發挑戰是建立具有固定標題的表,即使使用者垂直捲動,該表也保持可見穿過桌體。在本文中,我們將使用兩種方法來探索此問題的解決方案:自訂 CSS 解決方案和基於 JavaScript 的解決方案。
自訂 CSS 解決方案
自訂 CSS 解決方案依賴表頭的絕對定位,並在表體滾動時保持其位置。可以使用以下 CSS:
#table-wrapper { position: relative; } #table-scroll { height: 250px; overflow: auto; margin-top: 20px; } #table-wrapper table { width: 100%; } #table-wrapper table thead th { position: absolute; top: 0; z-index: 2; height: 20px; width: 35%; border: 1px solid red; }
此方法只是將標題定位在表格主體上方,並在捲動期間保持其頂部位置。
JavaScript 解決方案
另一種方法利用 JavaScript 來修復標題並處理滾動行為。以下程式碼片段示範了這種方法:
// Get the table and its header var table = document.getElementById("table"); var header = table.querySelector("thead"); // Clone the header var cloneHeader = header.cloneNode(true); // Create a wrapper div for the cloned header var headerWrapper = document.createElement("div"); headerWrapper.classList.add("header-wrapper"); // Insert the cloned header into the wrapper headerWrapper.appendChild(cloneHeader); // Insert the header wrapper into the table table.insertBefore(headerWrapper, table.firstChild); // Add event listener for scrolling table.addEventListener("scroll", function() { headerWrapper.style.left = table.scrollLeft + "px"; });
此JavaScript 方法複製標題,將其固定在表格頂部,並在表格滾動時更新其左側位置以保持與表格主體對齊.
兩個解決方案都有效解決了在可捲動div 中建立固定標題表的問題。方法的選擇取決於 Web 專案的特定要求和偏好。
以上是如何在可捲動 Div 中建立固定標題表?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction

重構自己的代碼看起來是什麼樣的?約翰·瑞亞(John Rhea)挑選了他寫的一個舊的CSS動畫,並介紹了優化它的思維過程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatoryand and powerincreatingsmoothcsssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingMultatingMultationMultationProperPertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用CombanningWiThjavoFofofofoftofofo

CSSCOUNTERSAREDOMANAGEAUTOMANAMBERINGINWEBDESIGNS.1)他們可以使用forterablesofcontents,ListItems,and customnumbering.2)AdvancedsincludenestednumberingSystems.3)挑戰挑戰InclassINCludeBrowsEccerCerceribaliblesibility andperformiballibility andperformissises.4)創造性

使用滾動陰影,尤其是對於移動設備,是克里斯以前涵蓋的一個微妙的UX。傑夫(Geoff)涵蓋了一種使用動畫限制屬性的新方法。這是另一種方式。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境