# Swiper Angular Components组件
# 安装
Swiper Angular插件仅作为主Swiper库的一部分通过NPM安装:
npm install swiper
# 模块导入
import { SwiperModule } from 'swiper/angular';
@NgModule({
imports: [SwiperModule],
})
export class YourAppModule {}
# 样式
Swiper包含不同的css,less和scss样式:
swiper/css
- 只包含Swiper核心样式swiper/css/bundle
- 所有Swiper模块样式(比如导航,分页样式等.)
模块样式 (如果已经导入了bundle样式,则不需要此选项):
swiper/css/a11y
-A11y
模块样式swiper/css/autoplay
-Autoplay
自动播放模块样式swiper/css/controller
-Controller
控制模块样式swiper/css/effect-cards
-Cards Effect
卡片特效模块样式swiper/css/effect-coverflow
-Coverflow Effect
覆盖流特效模块样式swiper/css/effect-creative
-Creative Effect
创意特效模块样式swiper/css/effect-cube
-Cube Effect
立体特效模块样式swiper/css/effect-fade
-Fade Effect
渐变模块样式swiper/css/effect-flip
-Flip Effect
翻转模块样式swiper/css/free-mode
-Free
自由模块样式swiper/css/grid
-Grid
网格模块样式swiper/css/hash-navigation
-Hash Navigation
哈斯导航模块样式swiper/css/history
-History module
历史模块样式swiper/css/keyboard
-Keyboard
键盘模块样式swiper/css/lazy
-Lazy
懒加载模块样式swiper/css/manipulation
-Manipulation
操作模块样式swiper/css/mousewheel
-Mousewheel
鼠标滚动样式swiper/css/navigation
-Navigation
导航模块样式swiper/css/pagination
-Pagination
分页模块样式swiper/css/parallax
-Parallax
视差模块样式swiper/css/scrollbar
-Scrollbar
滚动条样式swiper/css/thumbs
-Thumbs
缩略图模式swiper/css/virtual
-Virtual
虚拟模块样式swiper/css/zoom
-Zoom
放大模块样式
对于Less的样式,将css替换,例如:
import 'swiper/less';
import 'swiper/less/navigation';
import 'swiper/less/pagination';
对于SCSS的样式,将css替换,例如:
import 'swiper/scss';
import 'swiper/scss/navigation';
import 'swiper/scss/pagination';
# 用法
参数可以作为参数或通过[config]
传递到<swiper>
import { Component } from '@angular/core';
// import Swiper core and required modules
import SwiperCore from 'swiper';
@Component({
selector: 'app-swiper-example',
template: `
<swiper
[slidesPerView]="3"
[spaceBetween]="50"
(swiper)="onSwiper($event)"
(slideChange)="onSlideChange()"
>
<ng-template swiperSlide>Slide 1</ng-template>
<ng-template swiperSlide>Slide 2</ng-template>
<ng-template swiperSlide>Slide 3</ng-template>
</swiper>
`,
})
export class AppComponent {
onSwiper([swiper]) {
console.log(swiper);
}
onSlideChange() {
console.log('slide change');
}
}
使用 [config]:
import { Component } from '@angular/core';
// import Swiper core and required modules
import SwiperCore, { SwiperOptions } from 'swiper';
@Component({
selector: 'app-swiper-example',
template: `
<swiper
[config]="config"
(swiper)="onSwiper($event)"
(slideChange)="onSlideChange()"
>
<ng-template swiperSlide>Slide 1</ng-template>
<ng-template swiperSlide>Slide 2</ng-template>
<ng-template swiperSlide>Slide 3</ng-template>
<ng-template swiperSlide>Slide 4</ng-template>
<ng-template swiperSlide>Slide 5</ng-template>
<ng-template swiperSlide>Slide 6</ng-template>
</swiper>
`,
})
export class AppComponent {
config: SwiperOptions = {
slidesPerView: 3,
spaceBetween: 50,
navigation: true,
pagination: { clickable: true },
scrollbar: { draggable: true },
};
onSwiper([swiper]) {
console.log(swiper);
}
onSlideChange() {
console.log('slide change');
}
}
注意
默认情况下,Swiper Angular使用Swiper的核心版本(没有任何附加模块)。如果要使用导航、分页和其他模块,必须先安装它们。以下是其他模块导入列表:
Virtual
- 虚拟幻灯片模块Keyboard
- 键盘控制模块Mousewheel
- 鼠标滚动模块Navigation
- 导航模块Pagination
- 分布模块Scrollbar
- 滚动条模块Parallax
- 视差模块FreeMode
- 自由模式模块Grid
- 网格模块Manipulation
- 幻灯片操作模块(仅适用于核心版本)Zoom
- 放大模块Lazy
- 懒加载模块Controller
- 控制模块A11y
- 可达性控制模块History
- 历史导航模块HashNavigation
- Hash导航模块Autoplay
- 自动播放模块EffectFade
- 渐变特效模块EffectCube
- 立体特效模块EffectFlip
- 轻弹特效模块EffectCoverflow
- Coverflow Effect moduleEffectCards
- 卡片特效模块EffectCreative
- 创新特效模块Thumbs
- 缩略图模块
import { Component } from '@angular/core';
// import Swiper core and required modules
import SwiperCore, { Navigation, Pagination, Scrollbar, A11y } from 'swiper';
// install Swiper modules
SwiperCore.use([Navigation, Pagination, Scrollbar, A11y]);
@Component({
selector: 'app-swiper-example',
template: `
<swiper
[slidesPerView]="3"
[spaceBetween]="50"
(swiper)="onSwiper($event)"
(slideChange)="onSlideChange()"
[navigation]="true"
[pagination]="{ clickable: true }"
[scrollbar]="{ draggable: true }"
>
<ng-template swiperSlide>Slide 1</ng-template>
<ng-template swiperSlide>Slide 2</ng-template>
<ng-template swiperSlide>Slide 3</ng-template>
<ng-template swiperSlide>Slide 4</ng-template>
<ng-template swiperSlide>Slide 5</ng-template>
<ng-template swiperSlide>Slide 6</ng-template>
</swiper>
`,
})
export class AppComponent {
onSwiper([swiper]) {
console.log(swiper);
}
onSlideChange() {
console.log('slide change');
}
}
# 访问swiper引用
import { Component } from '@angular/core';
import { SwiperComponent } from "swiper/angular";
// import Swiper core and required modules
import SwiperCore, { Swiper, Virtual } from 'swiper';
// install Swiper modules
SwiperCore.use([Virtual]);
@Component({
selector: 'app-swiper-fade-example',
template: `
<swiper #swiper [virtual]="true">
<ng-template swiperSlide>Slide 1</ng-template>
<ng-template swiperSlide>Slide 2</ng-template>
<ng-template swiperSlide>Slide 3</ng-template>
<ng-template swiperSlide>Slide 4</ng-template>
<ng-template swiperSlide>Slide 5</ng-template>
<ng-template swiperSlide>Slide 6</ng-template>
<ng-template swiperSlide>Slide 7</ng-template>
</swiper>
<button (click)="slideNext()">Next slide</button>
<button (click)="slidePrev()">Prev slide</button>
`,
})
export class AppComponent {
@ViewChild('swiper', { static: false }) swiper?: SwiperComponent;
slideNext(){
this.swiper.swiperRef.slideNext(100);
}
slidePrev(){
this.swiper.swiperRef.slidePrev(100);
}
}
# SwiperComponent props
Swiper角组件将所有Swiper参数作为组件道具接收
# Swiper events事件
Swiper组件支持所有Swiper事件,包括尽快返回Swiper实例的附加Swiper事件。例如:使用EventsParams进行键入。参数始终作为数组返回,即使只传递了一个参数。
import { Component } from '@angular/core';
import { EventsParams } from 'swiper/angular';
@Component({
selector: 'app-swiper-example',
template: `
<swiper
(swiper)="onSwiper($event)"
(beforeTransitionStart)="onBeforeTransitionStart()"
>
<ng-template swiperSlide>Slide 1</ng-template>
<ng-template swiperSlide>Slide 2</ng-template>
<ng-template swiperSlide>Slide 3</ng-template>
</swiper>
`,
})
export class AppComponent {
onSwiper(params: EventsParams['swiper']) {
const [swiper] = params;
console.log(swiper);
}
beforeTransitionStart(params: EventsParams['beforeSlideChangeStart']) {
const [swiper, speed, internal] = params;
console.log('beforeTransitionStart, speed: ' + speed);
}
}
# SwiperSlideDirective props
Prop | 类型 | 默认 | 描述 |
---|---|---|---|
virtualIndex | number | 实际滑动指数。需要为虚拟幻灯片设置 |
<swiper>
<ng-template swiperSlide>
<div>Slide</div>
</ng-template>
<ng-template swiperSlide *ngFor="let slide of slides">
<div>Slide</div>
</ng-template>
</swiper>
# SwiperSlideDirective变量
SwiperSlideDirective导出以下变量:
isActive
- 当前幻灯片处于活动状态时为trueisPrev
- 当前幻灯片是活动的上一张幻灯片时为trueisNext
- 当前幻灯片是活动的下一张幻灯片时为真isVisible
- 当前幻灯片可见时为true(必须启用watchSlidesProgress Swiper参数)isDuplicate
- 例如,当当前幻灯片为重复幻灯片时(启用循环模式时),为true:
比如:
<swiper>
<ng-template swiperSlide let-data>
<div>Current slide is {{ data.isActive ? 'active' : 'not active' }}</div>
</ng-template>
</swiper>
# Slots插槽
Swiper Vue。js组件使用“插槽”进行内容分发。有4个插槽可用
container-start
- 元素将添加到swiper-container容器的开头container-end
(默认) - 元素将添加到swiper-container容器的尾部wrapper-start
- 元素将添加到swiper-wrapper容器的开头wrapper-end
- 元素将添加到swiper-wrapper容器的尾部
比如:
<swiper>
<ng-template swiperSlide>Slide 1</ng-template>
<ng-template swiperSlide>Slide 2</ng-template>
<span slot="container-start">Container Start</span>
<span slot="container-end">Container End</span>
<span slot="wrapper-start">Wrapper Start</span>
<span slot="wrapper-end">Wrapper End</span>
</swiper>
将被渲染成:
<div class="swiper">
<span slot="container-start">Container Start</span>
<div class="swiper-wrapper">
<span slot="wrapper-start">Wrapper Start</span>
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<span slot="wrapper-end">Wrapper End</span>
</div>
<span slot="container-end">Container End</span>
</div>
# Virtual Slides
此处的虚拟幻灯片渲染完全由Angular处理,除了设置[Virtual]=“true”之外,无需任何其他操作:
import { Component } from '@angular/core';
// import Swiper core and required modules
import SwiperCore, { Virtual } from 'swiper';
// install Swiper modules
SwiperCore.use([Virtual]);
@Component({
selector: 'app-swiper-virtual-example',
template: ` <swiper [slidesPerView]="3" [spaceBetween]="50" [virtual]="true">
<ng-container *ngFor="let slide of slides; index as i">
<ng-template swiperSlide>Slide {{ slide }}</ng-template>
</ng-container>
</swiper>`,
})
export class AppComponent {
// Create array with 1000 slides
slides = Array.from({ length: 1000 }).map(
(el, index) => `Slide ${index + 1}`
);
}
# Effects特效
以下效果可用:
Fade
Cube
Coverflow
Flip
Cards
Creative
要使用效果,必须先导入并安装它们(与所有其他模块一样)(示例):
import SwiperCore, { EffectFade } from 'swiper';
SwiperCore.use([EffectFade]);
# Effect 特效例子
import { Component } from '@angular/core';
// import Swiper core and required modules
import SwiperCore, { EffectFade, Swiper } from 'swiper';
// install Swiper modules
SwiperCore.use([EffectFade]);
@Component({
selector: 'app-swiper-fade-example',
template: `
<swiper effect="fade">
<ng-template swiperSlide>Slide 1</ng-template>
<ng-template swiperSlide>Slide 2</ng-template>
<ng-template swiperSlide>Slide 3</ng-template>
</swiper>
`,
})
export class AppComponent {}