2017-04-20 13 views
0

テーブルの行にカーソルを置いて半透明のオーバーレイを実現したいのですが、ここで少しDEMOを作成しました。ホバー上のテーブル行の背景色を変更する

しかし、私はテキストが何らかの形で変わることを望んでいないので、ちょうど背景と私がそれをオーバーレイのようなものにしたい理由は、セルを少し濃くしたいホバー上で同じ色。理にかなっている?

コード:

<table id="compTable" class="prTable"> 
    <tr class="prTableHeader"> 
     <th></th> 
     <th></th> 
     <th>1</th> 
     <th>2</th> 
     <th>3</th> 
    </tr> 
    <tr class="prTableRow"> 
     <td class="prExerVariNameTD blackColor">Squat</td> 
     <td class="prVerticalSpace"></td> 
     <td class="prTableCell" title="@finalDate">100</td> 
     <td class="prTableCell" title="@finalDate">100</td> 
     <td class="prTableCell" title="@finalDate">100</td> 
    </tr> 
</table> 

CSS

.prTableCell { 
    padding: 7px; 
    width: 60px; 
    text-align:center; 
    border:1px solid #e1e1e1; 
    cursor:default; 
} 

.prExerVariNameTD { 
    width: 200px; 
    border:1px solid #e1e1e1!important; 
    padding: 5px 5px 5px 10px; 
    background-color: white; 
} 

.prVerticalSpace { 
    width: 50px; 
} 

.rowHover { 
    /*background: rgba(204, 204, 204, 0.5);*/ 
    opacity: 0.5; 
} 

スクリプト:私が正しくあなたを理解していた場合

$(document).ready(function() { 
    $("table#compTable .prTableCell:even").css("background-color", "#eeeeee"); 
    $("table#compTable .prTableCell:odd").css("background-color", "White"); 

    $(".prTableRow").hover(function() { 
     $(this).toggleClass("rowHover"); 
    }); 
}); 
+0

CSSを使用してください:' .prTableRow:nth-​​child(even):hover {background-color:#eee; } .prTableRow:nth-​​child(奇数):ホバー{背景色:#fff; } ' –

+0

タイトルとコードは少し異なります。ホバー上の行の背景色を変更したいが、コードが表のセルの背景色を設定しているとします。 –

+0

私が最初に試したときには、私がホバリングしていたセルを変更しただけで、それはすべてちょうど混乱でした:/ @MikeMcCaughan –

答えて

1

のJavaScriptの必要はありません。レスキューにCSS!

これは、ホバーの背景色を少し暗く設定します。あなたは実際の色の値で遊ぶことができます色を濃くするか、軽くホバリングします。

.prTableCell { 
 
    padding: 7px; 
 
    width: 60px; 
 
    text-align: center; 
 
    border: 1px solid #e1e1e1; 
 
    cursor: default; 
 
} 
 

 
.prExerVariNameTD { 
 
    width: 200px; 
 
    border: 1px solid #e1e1e1!important; 
 
    padding: 5px 5px 5px 10px; 
 
    background-color: white; 
 
} 
 

 
.prVerticalSpace { 
 
    width: 50px; 
 
} 
 

 
.prTableCell:nth-child(even) { 
 
    background-color: #eee; 
 
} 
 

 
.prTableCell:nth-child(odd) { 
 
    background-color: #fff; 
 
} 
 

 
.prTableRow:hover .prTableCell:nth-child(even) { 
 
    background-color: #ddd; 
 
} 
 

 
.prTableRow:hover .prTableCell:nth-child(odd) { 
 
    background-color: #eee; 
 
}
<table id="compTable" class="prTable"> 
 
    <tr class="prTableHeader"> 
 
    <th></th> 
 
    <th></th> 
 
    <th>1</th> 
 
    <th>2</th> 
 
    <th>3</th> 
 
    </tr> 
 
    <tr class="prTableRow"> 
 
    <td class="prExerVariNameTD blackColor">Squat</td> 
 
    <td class="prVerticalSpace"></td> 
 
    <td class="prTableCell" title="@finalDate">100</td> 
 
    <td class="prTableCell" title="@finalDate">100</td> 
 
    <td class="prTableCell" title="@finalDate">100</td> 
 
    </tr> 
 
</table>

+0

':hover'をテーブル行からセルに移動することで、現在のセルだけをターゲットにすることができます。 –

+0

これは素晴らしいです!大いに感謝する! –

1

、あなたはそれを除いて(あなたのrgba背景と非常に接近していましたセルではなく行をターゲットにしていました)。これはあなたが探しているものですか?

$(document).ready(function() { 
 
    $("table#compTable .prTableCell:even").css("background-color", "#eeeeee"); 
 
    $("table#compTable .prTableCell:odd").css("background-color", "White"); 
 

 
    $(".prTableRow").hover(function() { 
 
     $(this).toggleClass("rowHover"); 
 
    }); 
 
});
.prTableCell { 
 
    padding: 7px; 
 
    width: 60px; 
 
    text-align:center; 
 
    border:1px solid #e1e1e1; 
 
    cursor:default; 
 
} 
 

 
.prExerVariNameTD { 
 
    width: 200px; 
 
    border:1px solid #e1e1e1!important; 
 
    padding: 5px 5px 5px 10px; 
 
    background-color: white; 
 
} 
 

 
.prVerticalSpace { 
 
    width: 50px; 
 
} 
 

 
.rowHover td:not(.prVerticalSpace) { 
 
    background: rgba(204, 204, 204, 0.5) !important; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="compTable" class="prTable"> 
 
    <tr class="prTableHeader"> 
 
     <th></th> 
 
     <th></th> 
 
     <th>1</th> 
 
     <th>2</th> 
 
     <th>3</th> 
 
    </tr> 
 
    <tr class="prTableRow"> 
 
     <td class="prExerVariNameTD blackColor">Squat</td> 
 
     <td class="prVerticalSpace"></td> 
 
     <td class="prTableCell" title="@finalDate">100</td> 
 
     <td class="prTableCell" title="@finalDate">100</td> 
 
     <td class="prTableCell" title="@finalDate">100</td> 
 
    </tr> 
 
</table>

+0

ああ、クールで、それらのすべてが終わることはありません同じ色?彼らはまだお互いに見えるようにオリジナルより暗いちょうど少しおかしいですか? –

0

annswerでhttps://jsfiddle.net/v9jphr5h/

tr:nth-child(2n) 
{ 
    background-color:#ffe; 
} 

tr:nth-child(n+2):hover>td, 
tr:nth-child(n+2):hover>td.prTableCell[title]{ 
    background: rgba(204, 204, 204, 0.5) 
} 

ボトムライン: - あなたのRGBA-アプローチは 作品 - jqueryの のために絶対に必要はありません - (あなたはとの互換性にしない限りしかし、その場合、rgba eithterを使うことはできません。その場合は、すべてのセルの色を計算し、「奇数」と「偶数"行をクラスに変換します)

関連する問題