ngx-bootstrap - 輪播圖



ngx-bootstrap 輪播圖用於建立影像或文字幻燈片。

CarouselComponent

建立輪播圖的基本元素。

選擇器

  • carousel

輸入

  • activeSlide − 數字,當前顯示幻燈片的索引(從 0 開始)

  • indicatorsByChunk − 布林值,預設值:false

  • interval − 數字,專案迴圈的延遲(毫秒)。如果為 false,輪播圖將不會自動迴圈。

  • isAnimated − 布林值,啟用/停用動畫。動畫不適用於多列表輪播圖,預設值:false

  • itemsPerSlide − 數字,預設值:1

  • noPause − 布林值

  • noWrap − 布林值

  • pauseOnFocus − 布林值

  • showIndicators − 布林值

  • singleSlideOffset − 布林值

  • startFromIndex − 數字,預設值:0

輸出

  • activeSlideChange − 當活動幻燈片更改時發出。雙向繫結 [(activeSlide)] 屬性的一部分

  • slideRangeChange − 在多列表模式下,當活動幻燈片更改時發出

SlideComponent

選擇器

  • slide

輸入

  • active − 布林值,當前幻燈片是否處於活動狀態

示例

由於我們將使用輪播圖,我們需要更新在ngx-bootstrap 按鈕章節中使用的 app.module.ts 以使用CarouselModule

更新 app.module.ts 以使用 CarouselModule。

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TestComponent } from './test/test.component';
import { AccordionModule } from 'ngx-bootstrap/accordion';
import { AlertModule,AlertConfig } from 'ngx-bootstrap/alert';
import { ButtonsModule } from 'ngx-bootstrap/buttons';
import { FormsModule } from '@angular/forms';
import { CarouselModule } from 'ngx-bootstrap/carousel';

@NgModule({
   declarations: [
      AppComponent,
      TestComponent
   ],
   imports: [
      BrowserAnimationsModule,
      BrowserModule,
      AccordionModule,
      AlertModule,
      ButtonsModule,
      FormsModule,
      CarouselModule
   ],
   providers: [AlertConfig],
   bootstrap: [AppComponent]
})
export class AppModule { }

更新 test.component.html 以使用輪播圖。

test.component.html

<div style="width: 500px; height: 500px;">
   <carousel [noWrap]="noWrapSlides" [showIndicators]="showIndicator">
      <slide *ngFor="let slide of slides; let index=index">
         <img [src]="slide.image" alt="image slide" style="display: block; width: 100%;">
         <div class="carousel-caption">
			<h4>Slide {{index}}</h4>
            <p>{{slide.text}}</p>
         </div>
      </slide>
   </carousel>
   <br/>
   <div>
      <div class="checkbox">
         <label><input type="checkbox" [(ngModel)]="noWrapSlides">Disable Slide Looping</label>
         <label><input type="checkbox" [(ngModel)]="showIndicator">Enable Indicator</label>
      </div>
   </div>
</div>

更新 test.component.ts 中相應的變數和方法。

test.component.ts

import { Component, OnInit } from '@angular/core';
import { CarouselConfig } from 'ngx-bootstrap/carousel';

@Component({
   selector: 'app-test',
   templateUrl: './test.component.html',
   providers: [
      { provide: CarouselConfig, useValue: { interval: 1500, noPause: false, showIndicators: true } }
   ],
   styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
   slides = [
      {image: 'assets/images/nature/1.jpg', text: 'First'},
      {image: 'assets/images/nature/2.jpg',text: 'Second'},
      {image: 'assets/images/nature/3.jpg',text: 'Third'}
   ];
   noWrapSlides = false;
   showIndicator = true;
   constructor() { }

   ngOnInit(): void {
   }
}

構建和執行

執行以下命令啟動 Angular 伺服器。

ng serve

伺服器啟動並執行後,開啟 https://:4200 並驗證以下輸出。

Carousel
廣告