-
Использование Angular Renderer2:
import { Component, ElementRef, Renderer2 } from '@angular/core'; @Component({ selector: 'app-scroll', template: ` <div class="scroll-container" #scrollContainer> <!-- Content here --> </div> ` }) export class ScrollComponent { constructor(private elementRef: ElementRef, private renderer: Renderer2) {} scrollToElement(elementId: string): void { const element = this.elementRef.nativeElement.querySelector(`#${elementId}`); this.renderer.scrollIntoView(element, { behavior: 'smooth' }); } } -
Использование ViewChild Angular:
import { Component, ViewChild, ElementRef } from '@angular/core'; @Component({ selector: 'app-scroll', template: ` <div class="scroll-container" #scrollContainer> <!-- Content here --> </div> ` }) export class ScrollComponent { @ViewChild('scrollContainer', { static: true }) scrollContainer: ElementRef; scrollToElement(elementId: string): void { const element = this.scrollContainer.nativeElement.querySelector(`#${elementId}`); element.scrollIntoView({ behavior: 'smooth' }); } } -
Использование директивы
scrollToUI Bootstrap:<div class="scroll-container" uib-scroll-to="elementId" uib-scroll-to-duration="1000"> <!-- Content here --> </div>
Это всего лишь несколько примеров того, как можно добиться плавной прокрутки в Angular с помощью UI Bootstrap. Не забудьте импортировать необходимые модули и зависимости в соответствии с настройками вашего проекта.