【发布时间】:2017-12-22 10:06:37
【问题描述】:
我在 div *ngFor 循环中有项目,它在选定的消息上应用 css 类 'message-list-active'。
app.html
<div id="listText" *ngFor="let message of messages; let i=index" activeIndex = i"
[ngClass]="{'message-list-active': activeIndex === i }">
{{message.name}}
</div>
component.ts //监听键盘事件,使用上下箭头选择消息
messages; // we have date stored here
activeIndex = 0;
@HostListener("document:keydown", ['$event'])
doSomething(event: KeyboardEvent): void {
if (event.code == "ArrowUp" && this.activeIndex > 0) {
this.activeIndex--
}
if (event.code == "ArrowDown" && this.activeIndex < this.messages.length-1) {
this.activeIndex++
}
}
app.css
#listText {
width: auto;
height:100%;
overflow: auto
}
.message-list-active {
margin: 0;
padding: 15px 15px 5px 13px;
border-radius: 0;
background-color: #CDE6F7;
border-style: dotted;
border-width: 1px;
border-bottom: 1px dotted;
}
问题是当所选消息到达列表末尾时,它不会滚动。所以我想要实现的是让所选项目始终保持焦点并能够向下和向上滚动:
这里也有类似的例子:
这是我试图实现的目标,这是使用 jquery jsfiddle http://jsfiddle.net/38zR3/42/、similar question/answer 回答的,但我如何使用 typescript 在 Angular 4 中实现这一目标
【问题讨论】:
标签: javascript css angular typescript keyboard-events