【发布时间】:2021-08-11 15:08:34
【问题描述】:
我正在使用 SwiperJs v6.6.1 在我的 Angular 12 应用程序中创建一个滑块。
这是我的 Angular 组件
import SwiperCore, {
Pagination,
Navigation,
EffectCoverflow,
} from 'swiper/core';
import { Component, ViewEncapsulation, ViewChild, OnInit } from '@angular/core';
import { SwiperComponent } from 'swiper/angular';
SwiperCore.use([Pagination, Navigation, EffectCoverflow]);
declare var $: any;
@Component({
selector: 'app-eg-image',
templateUrl: './eg-image.component.html',
styleUrls: ['./eg-image.component.scss'],
encapsulation: ViewEncapsulation.None,
})
export class EgImageComponent implements OnInit {
@ViewChild('swiperRef', { static: false }) sliderRef?: SwiperComponent;
public thumbsSwiper: any;
public swiper: any;
public slides = [
'https://swiperjs.com/demos/images/nature-10.jpg',
'https://swiperjs.com/demos/images/nature-2.jpg',
'https://swiperjs.com/demos/images/nature-3.jpg',
'https://swiperjs.com/demos/images/nature-4.jpg',
'https://swiperjs.com/demos/images/nature-5.jpg',
'https://swiperjs.com/demos/images/nature-6.jpg',
'https://swiperjs.com/demos/images/nature-7.jpg',
'https://swiperjs.com/demos/images/nature-8.jpg',
];
public activeImage: any = this.slides[0];
ngOnInit() {
this.swiper = {
autoHeight: 'true',
effect: 'coverflow',
spaceBetween: 50,
grabCursor: 'true',
centeredSlides: 'true',
slidesPerView: 'auto',
coverflowEffect: {
rotate: 50,
stretch: 0,
depth: 100,
modifier: 1,
slideShadows: true,
},
pagination: {
dynamicBullets: true,
el: '.swiper-pagination',
type: 'fraction',
},
navigation: true,
};
$('#bg-image').css(
'background-image',
'url(https://swiperjs.com/demos/images/nature-10.jpg)'
);
}
onSlideChange(event: any) {
this.activeImage = this.slides[event.realIndex];
$('#bg-image').css('background-image', 'url(' + this.activeImage + ')');
}
onSlideEnd(event: any) {
console.log('onSlideEnd', event);
this.appendSlides();
}
appendSlides() {
this.sliderRef!.swiperRef.appendSlide([
`<div class="swiper-slide"><img class="swiper-slide1" src="${this.slides[0]}" /></div>`,
]);
}
setThumbsSwiper(swiper: any) {
this.thumbsSwiper = swiper;
}
}
这是我的 HTML 代码,
<div class="swiper-container">
<div id="bg-image"></div>
<div class="swiper-wrapper">
<swiper
[config]="swiper"
(slideChange)="onSlideChange($event)"
(reachEnd)="onSlideEnd($event)"
class="mySwiper"
>
<ng-template swiperSlide *ngFor="let img of slides">
<img class="swiper-slide1" data-src="{{ img }}" />
<!-- <div class="swiper-lazy-preloader"></div> -->
</ng-template>
</swiper>
</div>
<div class="swiper-pagination"></div>
</div>
我正在尝试通过在到达最后一张幻灯片后添加新幻灯片来更新滑块,但 appendSlides() 方法没有更新滑块。
我也尝试更新 slides 数组
appendSlides() {
this.slides.push('https://swiperjs.com/demos/images/nature-8.jpg')
}
但它不起作用。有人可以指出我正确的方向吗?
我正在关注此处提供的文档:
- https://swiperjs.com/swiper-api#virtual-slides
- https://swiperjs.com/angular#virtual-slides
- https://swiperjs.com/demos#virtual-slides
这是 Stackblitz 网址:
https://stackblitz.com/edit/angular-ivy-52d8kq?file=src/app/app.component.ts
【问题讨论】:
-
swiper.js 是否与 Angular 兼容?还是它的纯js库?
-
@GaurangDhorda 根据他们的文档,它是兼容的