gpt4 book ai didi

angular - 如何根据 Angular 2中的按钮单击移动div滚动位置

转载 作者:太空狗 更新时间:2023-10-29 17:21:47 25 4
gpt4 key购买 nike

在 app.component.html 文件中,我有一个带有水平滚动条的 div 元素和两个按钮,分别是 Next 和 Previous。基于这些按钮点击我想移动滚动条。

app.component.html

<div id="th-infinite-scroll-tracker" style="overflow-y:scroll; height: 200px;" scrollTracker (scroll)="onScroll($event)" (mouseup)="searchLog($event)">
<div *ngFor="let log of arr; let i = index" innerHTML="{{log}}" [id]="i"></div>
</div>
<button (click)="onPreviousSearchPosition()" [disabled]="disablePreviousBtn">Previous</button>
<button (click)="onNextSearchPosition()" [disabled]="disableNextBtn">Next</button>

app.component.ts

@HostListener('scroll', ['$event'])
onScroll(event){
this.scrollObject = event;
}
onPreviousSearchPosition(){
this.disableNextBtn = false;

this.scrollObject.target.scrollTop = 20 * --this.idCount;
}
onPreviousNextPosition(){
this.disableNextBtn = false;

this.scrollObject.target.scrollTop = 20 * ++this.idCount;
}

使用上面的代码,我们可以移动滚动条,但我们需要手动滚动后获取的滚动事件对象。请建议我,如何在组件类中创建滚动事件对象

最佳答案

这就是滚动 div 元素的方式 - https://plnkr.co/edit/2v0iVgkOZfISqlFAkrNX?p=preview

例子:

import { Component, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div #panel style="overflow-y:scroll; height: 20px;" >
<div *ngFor="let log of arr; let i = index" innerHTML="{{log}}" [id]="i"></div>
</div>
<button (click)="onPreviousSearchPosition()">Previous</button>
<button (click)="onNextSearchPosition()">Next</button>
`
})
export class AppComponent {
public arr = ['foo', 'bar', 'baz'];
@ViewChild('panel', { read: ElementRef }) public panel: ElementRef<any>;

public onPreviousSearchPosition(): void {
this.panel.nativeElement.scrollTop -= 20;
}

public onNextSearchPosition(): void {
this.panel.nativeElement.scrollTop += 20;
}
}

关于angular - 如何根据 Angular 2中的按钮单击移动div滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43916827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com