出现滚动条时,css 伪元素(tr 外的三 Angular 形)位置未对齐

标签 css angularjs scrollbar

我有一个高度固定的面板,overflow-y:auto; 在这个面板中我显示表格,当用户点击其中一行时,行右侧的三 Angular 形出现工作正常,直到滚动条不出现在表格列表中。如果有滚动条,则面板下方会出现右箭头。如何解决这个问题?

这是 Working Fiddle我还在下面添加了完整的代码片段和有问题的图像 relevant image

(function() {

  'use strict';

  angular
    .module('app', [])
    .controller('TableController', function($log, $scope) {
      $scope.tables = [{
          "name": "one_table",
          "purpose": "test"
        },
        {
          "name": "one_",
          "purpose": "test"
        }, {
          "name": "two_",
          "purpose": "test"

        }, {
          "name": "three_",
          "purpose": "test"
        }, {
          "name": "four_",
          "purpose": "test"
        }, {
          "name": "five_",
          "purpose": "test"
        }, {
          "name": "six_",
          "purpose": "test"
        }, {
          "name": "seven_",
          "purpose": "test"
        }, {
          "name": "eight_",
          "purpose": "test"
        }, {
          "name": "nine_",
          "purpose": "test"
        }, {
          "name": "ten_",
          "purpose": "test"
        }
      ];
      $scope.tindex = -1;
      $scope.rowClicked = function(idx) {
        $scope.tindex = idx;
      }
    });
})();
.panel-body {
  display: block;
  height: 230px;
  overflow-x: hidden;
  overflow-y: auto;
}

table {
  width: 100%;
  max-width: 100%;
}

.arrow-left:after {
  border-bottom: 20px solid transparent;
  border-left: 20px solid #eee;
  border-right: 20px solid transparent;
  border-top: 20px solid transparent;
  clear: both;
  content: '';
  float: right;
  height: 0px;
  margin: 1px auto;
  position: absolute;
  right: 8px;
  width: 0px;
}
<!DOCTYPE html>
<html ng-app="app">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>

<body ng-controller="TableController">
  <div class="row col-md-12">
    <div class="col-md-4" data-define="tables">
      <div class="panel panel-default">
        <div class="panel-heading">
          <span>Tables</span>
        </div>
        <div class="panel-body no-padding">
          <table class="table table-striped">
            <tbody>
              <tr ng-repeat="table in tables track by $index" ng-click="rowClicked($index)" ng-class="tindex === $index ? 'arrow-left' : ''">
                <td>{{table.name}}</td>
                <td>{{table.purpose}}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

</body>

</html>

请帮我找到正确的方法。

另一方面,我们可以使用 angular-custom 指令来实现吗?

最佳答案

伪元素似乎没有正确定位,即使 tr 被赋予了 position: relative

这个问题可以通过向 td 添加伪元素来解决。

<tr ng-repeat="table in tables track by $index" >
   <td ng-click="rowClicked($index)" ng-class="tindex === $index ? 'arrow-left' : ''">{{table.name}}</td>
</tr>

并稍微改变 CSS:

table tbody tr td {
  position: relative;
}

.arrow-left:after {
    border-bottom: 20px solid transparent;
    border-left: 20px solid #eee;
    border-right: 20px solid transparent;
    border-top: 20px solid transparent;
    clear: both;
    content: '';
    float: right;
    height: 0px;
    margin: 1px auto;
    position: absolute;
    top: -3px;
    right: -18px;
    width: 0px;
}

不过,我不确定将点击事件移动到 td 是否适合您。

jsbin

更新

要使用多个 td,请按如下方式更新 CSS:

table tbody tr td {
  position: relative;
}

.arrow-left td:last-of-type:after {
    border-bottom: 20px solid transparent;
    border-left: 20px solid #eee;
    border-right: 20px solid transparent;
    border-top: 20px solid transparent;
    clear: both;
    content: '';
    float: right;
    height: 0px;
    position: absolute;
    top: -2px;
    right: 0;
    width: 0px;
}

jsbin

关于出现滚动条时,css 伪元素(tr 外的三 Angular 形)位置未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43046672/

相关文章:

jquery - jqTransform - 组合框选择的文本颜色更改问题

html - 是否可以使用 CSS 显示没有背景的图像?

javascript - 类没有对 Angular 其他页面采取行动

javascript - 图表显示两行相同的 ID 和日期

c# - 从富文本框控件获取当前滚动位置?

html - CSS flex column wrap with no height preset and forcing of number of columns

java - javaFX中具有不同大小节点的TreeTableView

javascript - Angularjs ng-include 不包括任何 View

css - 具有最大宽度而不会导致出现垂直滚动条

html - 滚动条显示在窗口上而不是我的 div 上