css pseudo element (triangle outside the tr) position misaligned when scrollbar appears

前端 未结 5 836
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 07:52

I have a panel which height is fixed and overflow-y:auto; in this panel I am displaying table and when user click on one of the row, triangle on right side of

5条回答
  •  遇见更好的自我
    2021-01-18 08:19

    Finally, I have resolved this by custom directive and little bit CSS and with help of my colleague.

    add a custom directive in panel-body and calculate the position of clicked row and the panel height and reposition the arrow class ( without any pseudo element). Below is full code

    (function() {
    
        'use strict';
    
        angular
            .module('app',[])
            .controller('TableController', function($log, $scope) {
            $scope.tables = [
            {
              "name": "one_table",
              "columns": [
                {"name": "id"},{"name": "f_name"}           
              ]
            },
            {
              "name": "one_",
              "columns": []
            }, {
              "name": "two_",
              "columns": []
            }, {
              "name": "three_",
              "columns": []
            }, {
              "name": "four_",
              "columns": []
            }, {
              "name": "five_",
              "columns": []
            }, {
              "name": "six_",
              "columns": []
            }, {
              "name": "seven_",
              "columns": []
            }, {
              "name": "eight_",
              "columns": []
            }, {
              "name": "nine_",
              "columns": []
            }, {
              "name": "ten_",
              "columns": []
            }
          ];
          $scope.tindex = -1;
          $scope.rowClicked = function (idx) {
            $scope.tblRowClicked = idx;
            $scope.tindex = idx;
          }
        }).directive('showArrow', function($window, $timeout) {
                return {
                    restrict: 'A',
                    link: function(scope, element) {
                        // change the arrow position
                        var minHeight, maxHeight, maxWidth, tableHeight, style;
                        var row, rowPos, arrow;
                        var changeArrowPosition = function() {
                            $timeout(function() {
                                row = element.find('.hover');
                                rowPos = row.position(); // get position of clicked row
                                //console.log("rowPos:minHeight:maxHeight:tableHeight", rowPos, minHeight, maxHeight,tableHeight);
                                arrow = element.children('.dir-right');
                                arrow.hide();
                                if (rowPos) {
                                    if (rowPos.top >= minHeight && rowPos.top <= maxHeight) {
                                        style = {"top": rowPos.top + "px"};
                                        arrow.css(style);
                                        // if table height is lesser than panel height
                                        if (tableHeight <= maxHeight && maxWidth > 435) {
                                            style = {"margin": "auto 5px"};
                                            arrow.css(style);
                                        }
                                        arrow.addClass('arrow-right').show();
                                    }
                                }
                            });
                        };
    
                        element.on("click scroll", function() {
                            var elem = angular.element(this);
                            maxHeight = elem.height(); // panel height
                            maxWidth = elem.width(); //panel width
                            tableHeight = elem.children('table').height(); // table height
                            minHeight = elem.children('table').find('tr').eq(0).height(); // firt row height
                            changeArrowPosition();
                        });
                    }
                };
            });
        
    })();
    .panel-body {
        display: block;
        height: 230px;
        padding: 0px !important;
        overflow-x: hidden;
        overflow-y: auto;
    }
    table {
      width:100%;
      max-width: 100%;
    }
    tr {
      cursor: pointer;
    }
    tr.hover {
      background-color: #e5ee4f!important;
    }
    .arrow-right {
        border-bottom: 20px solid transparent;
        border-left: 20px solid #e5ee4f;
        border-right: 20px solid transparent;
        border-top: 20px solid transparent;
        clear: both;
        content: '';
        float: right;
        height: 0px;
        left: 96%;
        margin: 0px auto;
        position: absolute;
        width: 0px;
    }
    
    .dir-right {
        display: none;
    }
    
    
    
      
      
      JS Bin
    
    
    
    
    
    
    
    Tables
    {{table.name}}

    Here is a Working Fiddle

    Hope it helps someone.

提交回复
热议问题