ホームページ  >  記事  >  ウェブフロントエンド  >  TABLE 固定列を実装するための HTML+CSS+JS のベスト プラクティス (一般的なブラウザと互換性があります)_html/css_WEB-ITnose

TABLE 固定列を実装するための HTML+CSS+JS のベスト プラクティス (一般的なブラウザと互換性があります)_html/css_WEB-ITnose

WBOY
WBOYオリジナル
2016-06-21 09:14:131376ブラウズ

BS アーキテクチャのエンタープライズ レベルのアプリケーションでは、テーブルの数が大きい場合、ユーザーの一般的な要求は、最初のいくつかの重要な列を非常に適切に修正することです。一部の重量のある JS コンポーネント ライブラリにもこの機能がありますが、この機能を実現するより簡単な方法はありますか?この一般的な解決策は、静的な Web ページまたは単純な動的ページを作成する場合、ロジックは比較的単純で、技術的には複雑ではなく、簡単に実現できます。動的関数を多く含むコンポーネントは冗長なコードを大量に記述する必要があり、単純な関数であってもイベント処理などの多くのコードが必要になります。この方法は不格好です。柔軟性が低く、良い解決策ではありません。

さまざまなシナリオでの長期間の分析と研究と実験の結果、一般的に、位置決め計算方法を使用する解決策を見つけたので、以下にコードを掲載して説明します。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript">function divScroll(scrollDiv){    var scrollLeft = scrollDiv.scrollLeft;    document.getElementById("tableDiv_title").scrollLeft = scrollLeft;    document.getElementById("tableDiv_body").scrollLeft = scrollLeft;        }function divYScroll(scrollYDiv){    var scrollTop = scrollYDiv.scrollTop;    document.getElementById("tableDiv_y").scrollTop = scrollTop;    }function onwheel(event){    var evt = event||window.event;    var bodyDivY = document.getElementById("tableDiv_y");    var scrollDivY = document.getElementById("scrollDiv_y");    if (bodyDivY.scrollHeight>bodyDivY.offsetHeight){        if (evt.deltaY){            bodyDivY.scrollTop = bodyDivY.scrollTop + evt.deltaY*7;            scrollDivY.scrollTop = scrollDivY.scrollTop + evt.deltaY*7;        }else{            bodyDivY.scrollTop = bodyDivY.scrollTop - evt.wheelDelta/5;            scrollDivY.scrollTop = scrollDivY.scrollTop - evt.wheelDelta/5;        }    }}</script><style type="text/css">body {margin:0;padding:0;}table {border-collapse:collapse;border:0;border:none;}table td  {border:1px solid #000;overflow:hidden;padding:0 2px;}</style></head><body><div style="width:500px; position:relative; padding-right:18px;">   <div style="position:relative;height:368px;overflow:hidden;width:100%">    <div style="padding-left:108px; width:auto;  overflow:hidden; background:#f00;" id="tableDiv_title" >    <table border="0" cellspacing="0" cellpadding="0"  >      <tr>        <td  style="min-width:30px; max-width:30px; left:0; top:0; width:30px;  overflow:hidden; background-color:#f00;position:absolute;z-index:1;">000</td>        <td  style="min-width:74px; max-width:74px; left:30px; top:0; width:74px;  overflow:hidden; background-color:#f00;position:absolute;z-index:1;">自动表格</td>        <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>        <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>        <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>        <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>        <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>        <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>      </tr>      </table>      </div>      <div style="overflow:hidden; position:absolute;height:128px; width:100%;" id="tableDiv_y" onmousewheel="onwheel(event);" onwheel="onwheel(event);">       <div style="padding-left:108px; width:auto;overflow:hidden;" id="tableDiv_body">        <table border="0" cellspacing="0" cellpadding="0" >          <tr>            <td  style="min-width:30px; max-width:30px; left:0; width:30px; overflow:hidden; background-color:#fff;position:absolute;z-index:1;">001</td>            <td  style="min-width:74px; max-width:74px;  left: 30px; width: 74px; overflow:hidden; background-color:#fff;position:absolute;z-index:1;">自动表格</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>          </tr>           <tr>           <td  style="min-width:30px; max-width:30px; left:0; width:30px; overflow:hidden; background-color:#fff; position:absolute; z-index:1;">002</td>            <td  style="min-width:74px; max-width:74px; left:30px; width:74px; overflow:hidden;background-color:#fff; position:absolute; z-index:1;">自动表格</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>          </tr>          <tr>            <td  style="min-width:30px; max-width:30px; left:0; width:30px; overflow:hidden; background-color:#fff;position:absolute;z-index:1;">003</td>            <td  style="min-width:74px; max-width:74px;left: 30px; width: 74px; overflow:hidden;background-color:#fff;position:absolute;z-index:1;">自动表格</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>          </tr>          <tr>            <td  style="min-width:30px; max-width:30px; left:0; width:30px; overflow:hidden; background-color:#fff;position:absolute;z-index:1;">004</td>            <td  style="min-width:74px; max-width:74px;  left: 30px; width: 74px; overflow:hidden;background-color:#fff;position:absolute;z-index:1;">自动表格</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>          </tr>           <tr>            <td  style="min-width:30px; max-width:30px; left:0; width:30px; overflow:hidden; background-color:#fff;position:absolute;z-index:1;">005</td>            <td  style="min-width:74px; max-width:74px;  left: 30px; width: 74px; overflow:hidden;background-color:#fff;position:absolute;z-index:1;">自动表格</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>          </tr>           <tr>            <td  style="min-width:30px; max-width:30px; left:0; width:30px; overflow:hidden; background-color:#fff;position:absolute;z-index:1;">006</td>            <td  style="min-width:74px; max-width:74px;  left: 30px; width: 74px; overflow:hidden;background-color:#fff;position:absolute;z-index:1;">自动表格</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>          </tr>               <tr>            <td  style="min-width:30px; max-width:30px; left:0; width:30px; overflow:hidden; background-color:#fff;position:absolute;z-index:1;">007</td>            <td  style="min-width:74px; max-width:74px;  left: 30px; width: 74px; overflow:hidden;background-color:#fff;position:absolute;z-index:1;">自动表格</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>          </tr>           <tr>            <td  style="min-width:30px; max-width:30px; left:0; width:30px; overflow:hidden; background-color:#fff;position:absolute;z-index:1;">008</td>            <td  style="min-width:74px; max-width:74px;  left: 30px; width: 74px; overflow:hidden;background-color:#fff;position:absolute;z-index:1;">自动表格</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>          </tr>           <tr>            <td  style="min-width:30px; max-width:30px; left:0; width:30px; overflow:hidden; background-color:#fff;position:absolute;z-index:1;">009</td>            <td  style="min-width:74px; max-width:74px;  left: 30px; width: 74px; overflow:hidden;background-color:#fff;position:absolute;z-index:1;">自动表格</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>          </tr>            <tr>            <td  style="min-width:30px; max-width:30px; left:0; width:30px; overflow:hidden; background-color:#fff;position:absolute;z-index:1;">010</td>            <td  style="min-width:74px; max-width:74px;  left: 30px; width: 74px; overflow:hidden;background-color:#fff;position:absolute;z-index:1;">自动表格</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >56454自动</td>            <td  style="min-width: 100px; max-width: 100px;  width: 100px;" >最后一列</td>          </tr>                   </table>    </div>              </div>      <div style="background-color:#eee;overflow:hidden;top:150px; width:100%; z-index:2;position:absolute;">        <div style="margin-left:108px; width:auto;overflow-x:scroll;overflow-y:hidden;" onscroll='divScroll(this);'>            <div style="width:630px; height:1px;"></div>        </div>    </div>    </div>        <div id="scrollDiv_y" style="display:block; overflow-x:hidden; overflow-y:scroll; position:absolute; top:22px; right:0px; height:118px; padding-bottom:10px;" onscroll='divYScroll(this);'>            <div style="width:1px; height:194px;"></div>         </div>    </div>   </div></body></html>
总 I. 全体の構造:

ページの基本要素は div+table であり、2 つの DIV 層が外側にラップされて固定されています。 JS コントロールを介して位置を調整し、通常の DIV スクロール バーの効果をシミュレートします。

2.位置:

上部に配置される。垂直スクロール バーがあるときに正常に表示されるようにするには、テーブル本体の外側の DIV が絶対位置に設定されるため、スクロール バーも絶対位置に設定する必要があります。さらに、テーブル ヘッダー、テーブル本体、およびスクロール バーの内部 DIV は、margin-left 属性を使用して左マージンを制御し、固定列のオフセットを自由のままにします。

2.幅の計算:

計算されます。テーブルヘッダーとボディの内側、幅は自動、適応テーブル幅、外側のdivの幅は100%、最外divは右を制御します。内側のマージンはpadding-right属性によって変更され、垂直スクロールバーの位置が自由になります。

3. 身長の計算:

‐ のs
to

バーは、テーブルと同じ幅の DIV と、 1 ピクセルの高さは垂直スクロール バーにも当てはまります。この形式を採用する理由は、垂直スクロールバーをこのように処理した後、テーブルヘッダーとテーブル本体の DIV 幅を計算する必要がなくなり、水平スクロールバーがより美しくなるからです。それ以外の場合、スクロール バーがある場合、テーブルのヘッダーと水平スクロール バーを垂直スクロール バーの幅だけ移動する必要があります。この計算は複雑ではありませんが、場合によっては問題が発生します。ここでは拡張されません。


5. スクロール イベント:

テーブル本体のスクロール バーが非表示になっているため、マウス ホイールが機能しないため、マウス ホイール イベントを処理するには JS を使用する必要があります。この記事のサンプル コードは と互換性があります。一般的なブラウザ。 6. 利点と欠点の分析:


この記事の解決策は、クリアリングの原則に重点を置いています。実際には、それは非常に複雑です。

このソリューションの利点は、コンポーネントを使用する場合、HTML 構造がシンプルであるため、JS コントロール コードが非常にクリーンで保守が容易であることです。欠点は計算量が多すぎることです。このソリューションはコンポーネントの開発により適しており、静的ページは適していないと考えられます。


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。