Yog*_*ale 6 scroll scrollbar typescript angular2-template angular
在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>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码我们可以移动滚动但我们需要滚动事件对象,它将在手动滚动后得到.请建议我,如何在组件类中创建滚动事件对象
小智 17
这是滚动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;
}
}
Run Code Online (Sandbox Code Playgroud)
我更愿意使用scrollIntoView()方法.当我们点击相应的元素时,此方法在浏览器中提供平滑的滚动过渡效果.
@Component({
selector: 'my-app',
template: `
<div #panel class="some-class">
<div *ngFor="let log of arr; let i = index" innerHTML="{{log}}" [id]="i"></div>
</div>
<button (click)="moveToSpecificView()">Move</button>
`
})
export class AppComponent {
public arr = ['foo', 'bar', 'baz'];
@ViewChild('panel') public panel:ElementRef;
public moveToSpecificView()(): void {
setTimeout(() => {
this.panel.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'start' });
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28740 次 |
| 最近记录: |