CarouselCarousel displays content using a slide effect featuring responsive mode and various customization options.

Carousel - 图1

Documentation

Import

  1. import {CarouselModule} from 'primeng/carousel';
  2.  

Getting Started

Carousel requires a collection of items as its value and a ng-template content to display where each item can be accessed using the implicit variable.

  1. <p-carousel [value]="items">
  2. <ng-template let-item pTemplate="item">
  3. Content to display
  4. </ng-template>
  5. </p-carousel>
  6.  

Managing Data

DataTable uses setter based checking to realize if the underlying data has changed to update the UI so your data changes such as adding or removing a record should always create a new array reference instead of manipulating an existing array. For example, use slice instead of splice when removing an item or use spread operator instead of push method when adding an item.

Limiting Visible Items

Default number of visible items is 3, use numVisible option to customize this.

  1. <p-carousel numVisible="1">
  2.  

Effects

The easing function to use is "ease-out" by default and this can be customized using easing property. See here for possible alternative values.

  1. <p-carousel easing="easeOutStrong">
  2.  

SlideShow

Carousel can display the contents in a slideshow, for this purpose autoPlayInterval and circular attributes are used.

  1. <p-carousel circular="circular" autoplayInterval="3000">
  2.  

Responsive

Responsive mode is enabled by default causing carousel to switch between small and large viewport depending on the breakpoint value which is 560 initially.

Properties

NameTypeDefaultDescription
valuearraynullArray of data to display.
numVisiblenumber3Number of visible items per page.
firstVisiblenumber0Index of the first visible item.
headerTextstringnullText of the header section.
effectDurationany500Duration of the scrolling animation in milliseconds or a predefined value like "slow", "normal" and "fast".
circularbooleanfalseDefines continuous scrolling.
breakpointnumber560Breakpoint value in pixels to switch between small and large viewport.
responsivebooleantrueWhen defined, causes carousel to adjust its width based on screen size.
autoplayIntervalnumber0Time in milliseconds to have carousel start scrolling automatically.
easingstringease-outEasing animation to use for scrolling.
pageLinksnumber3Number of maximum page links to display. If total page count exceeds this value a dropdown is displayed instead.
stylestringnullInline style of the element.
styleClassstringnullInline style of the element.

Events

NameParametersDescription
onPageevent.page: New page indexCallback to invoke on page change.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-carouselContainer element.
ui-carousel-headerHeader element.
ui-carousel-header-titleHeader text.
ui-carousel-viewportViewport containing the items.
ui-carousel-buttonNavigator button at header.
ui-carousel-next-buttonNext page button at header.
ui-carousel-prev-buttonPrevious page button at header.
ui-carousel-page-linksPage links container.
ui-carousel-page-linkA page link.
ui-carousel-mobiledropdownCancel icon.
ui-carousel-itemCancel icon.

Dependencies

None.

Source

View on GitHub

  1. <p-toast [style]="{marginTop: '80px'}"></p-toast>
  2. <p-carousel headerText="Cars" [value]="cars">
  3. <ng-template let-car pTemplate="item">
  4. <div class="ui-grid ui-grid-responsive">
  5. <div class="ui-grid-row">
  6. <div class="ui-grid-col-12"><img src="assets/showcase/images/demo/car/{{car.brand}}.png" width="60"></div>
  7. </div>
  8. <div class="ui-grid-row">
  9. <div class="ui-grid-col-6">Vin</div>
  10. <div class="ui-grid-col-6">{{car.vin}}</div>
  11. </div>
  12. <div class="ui-grid-row">
  13. <div class="ui-grid-col-6">Year</div>
  14. <div class="ui-grid-col-6">{{car.year}}</div>
  15. </div>
  16. <div class="ui-grid-row">
  17. <div class="ui-grid-col-6">Color</div>
  18. <div class="ui-grid-col-6">{{car.color}}</div>
  19. </div>
  20. <div class="ui-grid-row">
  21. <div class="ui-grid-col-12">
  22. <button type="button" pButton icon="pi pi-search" (click)="selectCar(car)"></button>
  23. </div>
  24. </div>
  25. </div>
  26. </ng-template>
  27. </p-carousel>
  28.  
  1. export class CarouselDemo {
  2. cars: Car[];
  3. constructor(private messageService: MessageService) {
  4. this.cars = [
  5. {vin: 'r3278r2', year: 2010, brand: 'Audi', color: 'Black'},
  6. {vin: 'jhto2g2', year: 2015, brand: 'BMW', color: 'White'},
  7. {vin: 'h453w54', year: 2012, brand: 'Honda', color: 'Blue'},
  8. {vin: 'g43gwwg', year: 1998, brand: 'Renault', color: 'White'},
  9. {vin: 'gf45wg5', year: 2011, brand: 'VW', color: 'Red'},
  10. {vin: 'bhv5y5w', year: 2015, brand: 'Jaguar', color: 'Blue'},
  11. {vin: 'ybw5fsd', year: 2012, brand: 'Ford', color: 'Yellow'},
  12. {vin: '45665e5', year: 2011, brand: 'Mercedes', color: 'Brown'},
  13. {vin: 'he6sb5v', year: 2015, brand: 'Ford', color: 'Black'}
  14. ];
  15. }
  16. selectCar(car: Car) {
  17. this.messageService.add({severity: 'info', summary: 'Car Selected', detail: 'Vin:' + car.vin});
  18. }
  19. }
  20.