Angular 4で要素にスクロール!smoothScrollToとscrollToElementの徹底解説
Angular 4でクリック時に要素にスクロールする方法
smoothScrollTo
は、Angular Material モジュールに含まれている関数です。この関数は、指定された要素にスムーズにスクロールします。
import { Component, OnInit } from '@angular/core';
import { animate, state, style, transition, trigger } from '@angular/animations';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
animations: [
trigger('scrollAnimation', [
state('start', style({
opacity: 0
})),
state('end', style({
opacity: 1
})),
transition('start => end', animate('1000ms'))
])
]
})
export class AppComponent implements OnInit {
private element: HTMLElement;
constructor() {
this.element = document.getElementById('my-element');
}
ngOnInit() {
this.element.scrollIntoView({
behavior: 'smooth'
});
}
}
上記のコードでは、smoothScrollTo
関数を使って、my-element
というIDを持つ要素にスクロールしています。
scrollToElement
は、JavaScript のネイティブ関数です。この関数は、指定された要素にスクロールします。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
private element: HTMLElement;
constructor() {
this.element = document.getElementById('my-element');
}
ngOnInit() {
this.element.scrollIntoView();
}
}
Angular 4でクリック時に要素にスクロールするには、smoothScrollTo
と scrollToElement
の2つの方法があります。どちらの方法を使うかは、あなたのニーズによって異なります。
上記以外にも、以下のような方法で要素にスクロールすることができます。
@HostListener
デコレータを使って、クリックイベントを処理するRouter
モジュールを使って、特定の要素にスクロールするng-click
ディレクティブを使って、スクロール処理を実行する
smoothScrollTo を使う
<div id="my-app">
<button (click)="scrollToElement()">スクロール</button>
<div id="my-element">
<h1>ここにスクロールします</h1>
</div>
</div>
import { Component, OnInit } from '@angular/core';
import { animate, state, style, transition, trigger } from '@angular/animations';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
animations: [
trigger('scrollAnimation', [
state('start', style({
opacity: 0
})),
state('end', style({
opacity: 1
})),
transition('start => end', animate('1000ms'))
])
]
})
export class AppComponent implements OnInit {
private element: HTMLElement;
constructor() {
this.element = document.getElementById('my-element');
}
ngOnInit() {
}
scrollToElement() {
this.element.scrollIntoView({
behavior: 'smooth'
});
}
}
scrollToElement を使う
<div id="my-app">
<button (click)="scrollToElement()">スクロール</button>
<div id="my-element">
<h1>ここにスクロールします</h1>
</div>
</div>
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
private element: HTMLElement;
constructor() {
this.element = document.getElementById('my-element');
}
ngOnInit() {
}
scrollToElement() {
this.element.scrollIntoView();
}
}
実行方法
上記のコードをファイルに保存し、以下のコマンドを実行してアプリケーションを実行できます。
ng serve
ブラウザで http://localhost:4200
を開くと、アプリケーションが表示されます。
上記
その他のスクロール方法
<div id="my-app">
<button (click)="scrollToElement()">スクロール</button>
<div id="my-element">
<h1>ここにスクロールします</h1>
</div>
</div>
import { Component, OnInit, HostListener } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
private element: HTMLElement;
constructor() {
this.element = document.getElementById('my-element');
}
ngOnInit() {
}
@HostListener('click', ['$event'])
onClick(event: Event) {
this.element.scrollIntoView();
}
}
<div id="my-app">
<a routerLink="/my-element">スクロール</a>
<div id="my-element">
<h1>ここにスクロールします</h1>
</div>
</div>
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
constructor(private router: Router) {}
ngOnInit() {
this.router.events.subscribe((event) => {
if (event instanceof Scroll) {
this.element.scrollIntoView();
}
});
}
}
<div id="my-app">
<button (ng-click)="scrollToElement()">スクロール</button>
<div id="my-element">
<h1>ここにスクロールします</h1>
</div>
</div>
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
private element: HTMLElement;
constructor() {
this.element = document.getElementById('my-element');
}
ngOnInit() {
}
scrollToElement() {
this.element.scrollIntoView();
}
}
上記で紹介した方法以外にも、ライブラリやフレームワークを使って要素にスクロールすることができます。
スクロール方法は、あなたのニーズやアプリケーションの要件によって異なります。
javascript html angular