MCPcopy Create free account
hub / github.com/angular/components / _scrollToActiveItem

Method _scrollToActiveItem

docs/src/app/shared/carousel/carousel.ts:119–144  ·  view source on GitHub ↗

Scrolls an item into the viewport.

()

Source from the content-addressed store, hash-verified

117
118 /** Scrolls an item into the viewport. */
119 private _scrollToActiveItem() {
120 if (!this._isOutOfView(this.index)) {
121 return;
122 }
123
124 const itemsArray = this.items();
125 let targetItemIndex = this.index;
126
127 // Only shift the carousel by one if we're going forwards. This
128 // looks better compared to moving the carousel by an entire page.
129 if (this.index > 0 && !this._isOutOfView(this.index - 1)) {
130 targetItemIndex = itemsArray.findIndex((_, i) => !this._isOutOfView(i)) + 1;
131 }
132
133 this.position = itemsArray[targetItemIndex].element.nativeElement.offsetLeft;
134 this.list().nativeElement.style.transform = `translateX(-${this.position}px)`;
135 this.showPrevArrow = this.index > 0;
136 this.showNextArrow = false;
137
138 for (let i = itemsArray.length - 1; i > -1; i--) {
139 if (this._isOutOfView(i, 'end')) {
140 this.showNextArrow = true;
141 break;
142 }
143 }
144 }
145
146 /** Checks whether an item at a specific index is outside of the viewport. */
147 private _isOutOfView(index: number, side?: 'start' | 'end') {

Callers 3

onKeydownMethod · 0.95
nextMethod · 0.95
previousMethod · 0.95

Calls 1

_isOutOfViewMethod · 0.95

Tested by

no test coverage detected