首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以编程方式滚动表行

以编程方式滚动表行
EN

Stack Overflow用户
提问于 2017-03-16 05:36:00
回答 1查看 1.1K关注 0票数 2

这一击的目标是有一个表,在该表中,上下键将用于编程地选择行并滚动表。选定的行将具有不同的背景色。

在上下键时,我使用e.preventDefault()避免行向上/向下移动两次。问题是,当我开始向下滚动时,行保持固定,所选的行消失。怎么解决这个问题?

HTML

代码语言:javascript
运行
复制
<div id="selector" tabindex="0" ng-keydown="scroll($event)"
          style="width:300px;height:80px;border:1px solid gray;overflow-y:auto">
     <table>
        <tr ng-repeat="item in items">
            <td class="td1" ng-class="{'tdactive' : $index==index }">{{item.col}}</td>
            <td class="td1" ng-class="{'tdactive' : $index==index }">{{item.dsc}}</td>
        </tr>
     </table>
 </div>

Javascript

代码语言:javascript
运行
复制
var app = angular.module('app', []);

app.controller('ctl', function($scope) {

  document.getElementById("selector").focus();

  $scope.items = [ {col:"aaa", dsc:"AAA1"}, {col:"bbb", dsc:"BBB2"} , {col:"ccc", dsc:"CCC3"}, 
            {col:"aaa2", dsc:"AAA21"}, {col:"bbb2", dsc:"BBB22"} , {col:"ccc2", dsc:"CCC23"}, 
            {col:"aaa2", dsc:"AAA21"}, {col:"bbb2", dsc:"BBB22"} , {col:"ccc2", dsc:"CCC23"} ];
  $scope.index = 0;

  $scope.scroll = function(e) {
      if (e.which === 40) { // down arrow
          if ($scope.index<$scope.items.length - 1)
              $scope.index++;
          e.preventDefault();
      }
      else if (e.which === 38) { // up arrow
          if ($scope.index>0)
              $scope.index--;
          e.preventDefault();
      }
  };
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-16 06:56:14

首先,您需要将表行id添加为id="tr-{{$index}}"

如果tr位于当前视图中,则可以防止滚动。

代码语言:javascript
运行
复制
$scope.scroll = function(e) {
    var parentContainer = document.getElementById("selector");
      if (e.which === 40) { // down arrow
          if ($scope.index<$scope.items.length - 1)
          {

          var element = document.getElementById("tr-"+$scope.index);
          if(isElementInViewport(parentContainer,element)){
            e.preventDefault();
          }

              $scope.index++;
          }
      }
      else if (e.which === 38) { // up arrow
          if ($scope.index>0)
          {
          var element = document.getElementById("tr-"+$scope.index);
          if(!isElementInViewport(parentContainer,element)){
            e.preventDefault();
          }
              $scope.index--;
          }
      }
  };

function isElementInViewport(parent, el) {
  if(parent==undefined || el==undefined)
  return false;
    var elRect = el.getBoundingClientRect(),
        parRect = parent.getBoundingClientRect();
        //console.log(elRect)
        //console.log(parRect)
        var elementHeight = elRect.height;
    return (
        elRect.top >= parRect.top &&
        elRect.bottom <= parRect.bottom &&
        elRect.bottom+elementHeight<= parRect.bottom
    );
}

工作柱塞

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42826051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档