2017-05-11 7 views
1

app.component.htmlファイル内には、水平スクロールと2つのボタンがあり、次と前の2つのdiv要素があります。これらのボタンのクリックに基づいて私はスクロールを移動したい。私たちは、スクロールを移動することができますが、我々は後に取得するイベントオブジェクトをスクロールする必要があるコードの上に使用してdiv2のスクロール位置の移動方法angle2のボタンクリック

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; 
    } 

手動でスクロールします。 は私を提案してください、コンポーネントクラスでは、scrollイベントオブジェクトを作成する方法を

+0

多くのスクロールオブジェクトが必要ですか? –

+0

私たちが手動でスクロールすると、上のような$ eventオブジェクトがonScroll(イベント)としてコード内に取得されます。{ this.scrollObject = event; } –

+0

私は、コンポーネントクラスでスクロールイベントオブジェクトを作成する方法をお勧めしますか? - this.scrollObject = event; –

答えて

6

これはdiv要素をスクロールする方法である - https://plnkr.co/edit/2v0iVgkOZfISqlFAkrNX?p=preview

インポートから{コンポーネント、ViewChild、ElementRef} '@角度/コア';

@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; 
    } 
} 
+0

これは私のために働いています、ありがとうございましたRusev –

+1

@ YogeshUkaleもしそれが動作する場合、答えを受け入れるように誰でもそれに注意を払うことができます:) – SlimenTN

2

私は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' }); 
     }); 
    } 

    } 
+0

setImmediateを使用しないでください、非標準です。出典:https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate –

+0

@ChrisHainesありがとうございます。これをsetTimeoutメソッドに更新しました.. –

関連する問題