开发者社区> 问答> 正文

如何浏览溢出:滚动

1)当我使用键盘按键(38,40)在li中导航时,由于溢出,它不会滚动到突出显示的li:滚动。2)是否可以在输入键(13)的输入中添加突出显示的文本?

var pr = ["Product1", "Product2", "Product3", "Product4", "Product5", "Product6","Product1", "Product2", "Product3", "Product4", "Product5", "Product6","Product1", "Product2", "Product3", "Product4", "Product5", "Product6","Product1", "Product2", "Product3", "Product4", "Product5", "Product6","Product1", "Product2", "Product3", "Product4", "Product5", "Product6"];
for (var option in pr) {
  var newLi = document.createElement("li");
  newLi.innerHTML = pr[option];
  $("#here").append(newLi);
}

var currentFocus;

$(document).on("keyup", function(e) {
  if (e.keyCode === 38 || e.keyCode === 40) {
    e.preventDefault();
    
    var children = $("#here").children();
    if (currentFocus === undefined) {
      currentFocus = 0;
    } else {
      currentFocus += e.keyCode === 38 ? -1 : 1;
      currentFocus < 0 && (currentFocus = children.length - 1);
      currentFocus >= children.length && (currentFocus = 0);
    }
    children.removeClass("--focus");
    children.eq(currentFocus).addClass("--focus"); 
  }

});

#here {
  background-color: white;
  width: 175px;
  height: 300px;
  border: 1px solid grey;
  padding: 0px 0px 10px 10px;
  overflow: scroll;
}
#here li:hover,
#here li.--focus {
  background: #ccc;
  cursor: pointer;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input>
<ul id="here">
</ul>

展开
收起
垚tutu 2019-12-19 16:49:56 716 0
1 条回答
写回答
取消 提交回答
  • 关键在overflow-y:auto;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    5
    6
    7
    8
    9
    10
    2021-02-20 00:49:49
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载