简体   繁体   English

更改悬停表格行和列的背景

[英]Change background of table row and column on hover

Noobie at using angularJS . Noobie使用angularJS I have a bootstrap table populated using ng-repeat . 我有一个使用ng-repeat填充的bootstrap表。 It is a matrix layout, with row and column headers, how should I highlight the complete td row and column with cursor on respective table cell. 这是一个具有行和列标题的矩阵布局,如何在各个表单元格上用光标突出显示完整的td row and column

So far I have a class in main.css .tdHover{ background-color: red; } 到目前为止,我在main.css .tdHover{ background-color: red; } .tdHover{ background-color: red; } which I want to apply on hover. .tdHover{ background-color: red; }我想申请悬停。

Here is my html in jade : 这是我在jade html

 td(ng-repeat='game in games', ng-mouseover="mouseOverTd(game)", ng-class="{??}", style='text-align:center;vertical-align:middle;')

Controller: 控制器:

angular.module('theApp')
  .controller('MainCtrl', function ($scope, $http, socket) {
    $scope.games= [];
    $scope.idHoveredTD = null;

    $scope.mouseOverTd = function(game){
      window.alert(theme);
      //how should I apply/return class to apply?
    };
    //mouseout remove class?  

在此处输入图片说明

There are two ways to approach this. 有两种方法可以解决此问题。 One involves no JavaScript, but some slightly hacky CSS. 其中一个不涉及JavaScript,但涉及一些有点黑的CSS。 The other approach uses ng-mouseover as you were thinking. 您正在思考的另一种方法是使用ng-mouseover。 I prefer the CSS approach because it means my table's appearance is entirely controlled by CSS which feels tidier. 我更喜欢CSS方法,因为这意味着我的表格外观完全由感觉整洁的CSS控制。 Both approaches are presented below. 两种方法都在下面介绍。

Pure CSS Solution 纯CSS解决方案

You can actually affect the way the table looks when you hover using pure CSS - you don't need to use JavaScript at all. 当您使用纯CSS悬停时,实际上可以影响表的外观-根本不需要使用JavaScript。

To do that, add a class, say class="tablecell" to your td, and a similar one to your row. 为此,添加一个类,在您的td中添加class="tablecell" ,并在您的行中添加一个类似的类。 Next, add something like this to your main.css: 接下来,在您的main.css中添加以下内容:

.tablerow:hover, .tablecell:hover {
    background-color:red
}

So that's two thirds of a job done - rows and cells! 这样就完成了工作的三分之二-行和单元!

Columns are harder because they don't have a dedicated element to watch for hovering. 列比较难,因为它们没有专用的元素来监视悬停。 Instead, we can use a bit of a CSS hack - make a huge highlighting element, and clip the edges where it spills above and below the table. 取而代之的是,我们可以使用一些CSS技巧-制作一个巨大的突出显示元素,并修剪溢出到桌子上方和下方的边缘。

table { 
    overflow: hidden;
}
.tablecell {
    position:relative;
}

.tablecell:hover::before {
    content:"";
    position: absolute;
    left: 0;
    top: -5000px;
    height: 10000px;
    width: 100%;
    z-index: -1;
    /* keep it below table content */
    background-color: red;
}

Live Example: 现场示例:

Putting it all together, we get something like this: 放在一起,我们得到的是这样的:

 table { overflow: hidden; } .tablecell { position: relative; } .tablecell:hover::before { content: ""; position: absolute; left: 0; top: -5000px; height: 10000px; width: 100%; z-index: -1; /* keep it below table content */ background-color: red; } .tablerow:hover { background-color: red; } 
 <div ng-app="theApp" ng-controller="MyCtrl"> <table> <tr class="tablerow"> <td class="tablecell">aaa</td> <td class="tablecell">aaa</td> <td class="tablecell">aaa</td> </tr> <tr class="tablerow"> <td class="tablecell">bbb</td> <td class="tablecell">bbb</td> <td class="tablecell">bbb</td> </tr> <tr class="tablerow"> <td class="tablecell">ccc</td> <td class="tablecell">ccc</td> <td class="tablecell">ccc</td> </tr> </table> </div> 

More information on the column highlighting hack here. 有关在此处重点介绍hack的专栏的更多信息。


JavaScript Solution JavaScript解决方案

If you'd rather use JavaScript directly to avoid the above CSS hack, you can do that too. 如果您希望直接使用JavaScript来避免上述CSS hack,您也可以这样做。 Your mouseOverTd function then needs to log which row and column are currently being hovered over. 然后,您的mouseOverTd函数需要记录当前正在悬停的行和列。 The ng-class attribute then needs to check whether the currently hovered row and column matches this cell's row or column. 然后,ng-class属性需要检查当前悬停的行和列是否与此单元格的行或列匹配。

Something like this: 像这样:

angular.module("theApp", [])
    .controller("MainCtrl", function ($scope) {
        $scope.rows = [1, 2, 3, 4]
        $scope.games = ['a', 'b', 'c', 'd'];
        $scope.hoveredCol = null;
        $scope.hoveredRow = null;
        $scope.mouseOverTd = function (row, game) {
            $scope.hoveredRow = row;
            $scope.hoveredCol = game;
        };
    });

And your HTML (or rather Jade): 还有您的HTML(或更确切地说是Jade):

td(ng-repeat="game in games", ng-mouseover="mouseOverTd(row, game)" ng-class="{highlighted: (hoveredCol == game || hoveredRow == row)}") {{game}}

Of course, you then need to make sure to reset hoveredCol and hoveredRow when the mouse leaves the table, so also add something like this: 当然,然后,您需要确保在鼠标离开表格时重置hoveredColhoveredRow ,因此还要添加以下内容:

table(ng-mouseleave="hoveredCol = null; hoveredRow = null")

Live Example: 现场示例:

Putting that all into practice, we get something like this: 将所有这些付诸实践,我们得到的是这样的:

 angular.module("theApp", []) .controller("MainCtrl", function($scope) { $scope.rows = [1, 2, 3, 4] $scope.games = ['a', 'b', 'c', 'd']; $scope.hoveredCol = null; $scope.hoveredRow = null; $scope.mouseOverTd = function(row, game) { $scope.hoveredRow = row; $scope.hoveredCol = game; }; }); 
 td { padding: 10px; } .highlighted { background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="theApp" ng-controller="MainCtrl"> <table ng-mouseleave="hoveredCol = null; hoveredRow = null"> <tr ng-repeat="row in rows"> <td ng-repeat="game in games" ng-mouseover="mouseOverTd(row, game)" ng-class="{highlighted: (hoveredCol == game || hoveredRow == row)}">{{game}}</td> </tr> </table> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM