1. 程式人生 > >模仿Excel設定單元格 -- table單個TD的邊框顏色效果

模仿Excel設定單元格 -- table單個TD的邊框顏色效果

最近有這麼個需求,滑鼠點選td時需要變化td的顏色,由於一般都是適用了·border-collapse: collapse;這個屬性,故相鄰的兩個td共用一個邊框,先看一看設定不同值的效果:

td設定邊框效果

解決思路:直接設定行不通,設定2px又顯得太寬了,不過看了WPS的表格選中狀態時就是會比其它的要寬一倍的,而且行之間切換時td的高度會變化,會有跳動的感覺,所以當自身實現不了時,就得靠其它的元素來輔助了,上下左右分部用一個1px帶背景色的元素定位到td的四周,位置和元素的大小通過除錯了幾次,就找到剛剛好的值了。

<!DOCTYPE html>
<html lang="en">

<
head
>
<meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>設定單個TD邊框顏色</title> <style> * { box-sizing: border-box; margin
: 0; padding: 0; } table { border: 1px solid #ddd; background: #eff2f6; width: 100%; max-width: 100%; margin-bottom: 0; border-collapse: collapse; border-spacing: 0; } td, tr, th
{ border: 1px solid #ddd; padding: 10px 12px; text-align: center; } table td { position: relative; } table.demo1 td .line-sh { display: none; } table.demo1 td.active .line-sh { display: block; position: absolute; background-color: #016ac4; } table.demo1 td.active .line-sh.top { left: 0; top: -1px; width: calc(100% + 1px); height: 1px; } table.demo1 td.active .line-sh.right { right: -1px; top: 0; width: 1px; height: calc(100% + 1px); } table.demo1 td.active .line-sh.bottom { left: -1px; bottom: -1px; width: calc(100% + 1px); height: 1px; } table.demo1 td.active .line-sh.left { left: -1px; top: -1px; width: 1px; height: calc(100% + 1px); } table.demo1 td div { position: relative; z-index: 2; } table.demo2 td.active { border: 1px solid #016ac4; } table.demo3 td.active { border: 2px solid #016ac4; }
</style> </head> <body> <div style="width:800px;margin:20px auto;"> <div style="height:80px;padding-top:40px;"> 未經處理加1px邊框顏色效果</div> <table class="table table-bordered demo2"> <thead> <tr> <th>#</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td> <div>Mark</div> </td> <td> <div>Otto</div> </td> <td> <div>@mdo</div> </td> </tr> <tr> <th scope="row">2</th> <td> <div>Mark</div> </td> <td> <div>Otto</div> </td> <td> <div>@mdo</div> </td> </tr> <tr> <th scope="row">3</th> <td> <div>Mark</div> </td> <td> <div>Otto</div> </td> <td> <div>@mdo</div> </td> </tr> <tr> <th scope="row">4</th> <td> <div>Mark</div> </td> <td> <div>Otto</div> </td> <td> <div>@mdo</div> </td> </tr> </tbody> </table> <div style="height:80px;padding-top:40px;"> 未經處理加2px邊框顏色效果</div> <table class="table table-bordered demo3"> <thead> <tr> <th>#</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td> <div>Mark</div> </td> <td> <div>Otto</div> </td> <td> <div>@mdo</div> </td> </tr> <tr> <th scope="row">2</th> <td> <div>Mark</div> </td> <td> <div>Otto</div> </td> <td> <div>@mdo</div> </td> </tr> <tr> <th scope="row">3</th> <td> <div>Mark</div> </td> <td> <div>Otto</div> </td> <td> <div>@mdo</div> </td> </tr> <tr> <th scope="row">4</th> <td> <div>Mark</div> </td> <td> <div>Otto</div> </td> <td> <div>@mdo</div> </td> </tr> </tbody> </table> <div style="height:80px;padding-top:40px;">使用元素偽裝border效果</div> <table class="table table-bordered demo1"> <thead> <tr> <th>#</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td> <b class="line-sh top"></b><b class="line-sh right"></b> <b class="line-sh bottom"></b><b class="line-sh left"></b> <div>Mark</div> </td> <td> <b class="line-sh top"></b><b class="line-sh right"></b> <b class="line-sh bottom"></b><b class="line-sh left"></b> <div>Otto</div> </td> <td> <b class="line-sh top"></b><b class="line-sh right"></b> <b class="line-sh bottom"></b><b class="line-sh left"></b> <div>@mdo</div> </td> </tr> <tr> <th scope="row">2</th> <td> <b class="line-sh top"></b><b class="line-sh right"></b> <b class="line-sh bottom"></b><b class="line-sh left"></b> <div>Mark</div> </td> <td> <b class="line-sh top"></b><b class="line-sh right"></b> <b class="line-sh bottom"></b><b class="line-sh left"></b> <div>Otto</div> </td> <td> <b class="line-sh top"></b><b class="line-sh right"></b> <b class="line-sh bottom"></b><b class="line-sh left"></b> <div>@mdo</div> </td> </tr> <tr> <th scop