Open In App

Angular PrimeNG Carousel Responsive

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Basic Carousel in Angular PrimeNG.

Angular PrimeNG Basic Carousel is used to render the basic carousel. The carousel is a slider-type component that offers high customization.



Angular PrimeNG Carousel Responsive for responsive design based on screen size, numVisible, and numScroll can be defined using the responsiveOptions property which should be an array of objects whose breakpoint defines the max-width to apply the settings.

 



Syntax:

<p-carousel [value]="..." [responsiveOptions]="responsiveOptions">
    <ng-template let-car pTemplate="item">
        Statements
    </ng-template>
</p-carousel>

The syntax for creating responsiveOptions:

responsiveOptions : any[]=[
    {
        breakpoint: '1024px',
        numVisible: 3,
        numScroll: 3
    }
]

Creating Angular application & Module Installation:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2:  After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

 

Example 1: In this example, we will learn about carousel responsiveness.

app.component.html




<div id="GFG">
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>Angular PrimeNG Carousel Responsive</h2>
    <div style="width:80%;">
        <p-carousel [value]="images" [circular]="true"
                    [responsiveOptions]="responsiveOptions">
  
            <ng-template let-images pTemplate="item">
                <div class="product-item" style="width:80%">
                    <div class="product-item-content">
                        <img [src]="images.previewImageSrc" 
                            style="overflow: hidden;
                                   background-repeat: no-repeat;"
                            width="100%" height="100%" 
                            [alt]="images.alt" 
                            [title]="images.title">
                    </div>
                </div>
            </ng-template>
        </p-carousel>
    </div>
</div>

app.component.css




.product-item-content {
    border: 1px solid var(--surface-d);
    border-radius: 3px;
    margin: 0.3rem;
    text-align: center;
    padding: 2rem 0;
    background-color: snow;
}
  
.product-image {
    width: 50%;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16),
      0 3px 6px rgba(0, 0, 0, 0.23);
}
  
h3 {
    color: red;
    text-align: center;
}

app.component.ts




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'GFG';
  
    images: any[] = [
        {
            previewImageSrc:
            thumbnailImageSrc:
            alt: 'Cascading Style Sheet',
            title: 'CSS'
        },
        {
            previewImageSrc:
            thumbnailImageSrc:
            alt: 'Angular for Front end',
            title: 'Angular'
        },
        {
            previewImageSrc:
            thumbnailImageSrc:
            alt: 'Java Programming Language',
            title: 'Java'
        },
        {
            previewImageSrc:
            thumbnailImageSrc:
            alt: 'HyperText Markup Language',
            title: 'HTML'
        },
    ];
    responsiveOptions: any[] = [
        {
            breakpoint: '768px',
            numVisible: 2,
            numScroll: 2
        }
    ]
}

app.module.ts




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { CarouselModule } from 'primeng/carousel';
  
@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        BrowserModule,
        CarouselModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

Output:

 

Example 2: In this example, we will learn about carousel responsiveness. We will add more breakpoints in the carousel.

app.component.html




<div id="GFG">
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>Angular PrimeNG Carousel Responsive</h2>
    <div style="width:80%;">
        <p-carousel [value]="images" [circular]="true"
                    [responsiveOptions]="responsiveOptions">
            <ng-template let-images pTemplate="item">
                <div class="product-item" style="width:80%">
                    <div class="product-item-content">
                        <img [src]="images.previewImageSrc" 
                            style="overflow: hidden;
                                 background-repeat: no-repeat;"
                            width="100%" height="100%" 
                            [alt]="images.alt" 
                            [title]="images.title">
                    </div>
                </div>
            </ng-template>
        </p-carousel>
    </div>
</div>

app.component.css




.product-item-content {
    border: 1px solid var(--surface-d);
    border-radius: 3px;
    margin: 0.3rem;
    text-align: center;
    padding: 2rem 0;
    background-color: snow;
}
  
.product-image {
    width: 50%;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 
      0 3px 6px rgba(0, 0, 0, 0.23);
}
  
h3 {
    color: red;
    text-align: center;
}

app.component.ts




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'GFG';
  
    images: any[] = [
        {
            previewImageSrc:
            thumbnailImageSrc:
            alt: 'Cascading Style Sheet',
            title: 'CSS'
        },
        {
            previewImageSrc:
            thumbnailImageSrc:
            alt: 'Angular for Front end',
            title: 'Angular'
        },
        {
            previewImageSrc:
            thumbnailImageSrc:
            alt: 'Java Programming Language',
            title: 'Java'
        },
        {
            previewImageSrc:
            thumbnailImageSrc:
            alt: 'HyperText Markup Language',
            title: 'HTML'
        },
    ];
    responsiveOptions: any[] = [
        {
            breakpoint: '1024px',
            numVisible: 3,
            numScroll: 3
        },
        {
            breakpoint: '768px',
            numVisible: 2,
            numScroll: 2
        },
        {
            breakpoint: '560px',
            numVisible: 1,
            numScroll: 1
        }
    ]
}

app.module.ts




import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { CarouselModule } from 'primeng/carousel';
  
@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        BrowserModule,
        CarouselModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

Output:

 

Reference: http://primefaces.org/primeng/carousel


Article Tags :