我正在使用一个Bootstrap 5 carousel ,它带有data-bs-ride="carousel"属性来自动播放幻灯片.通过使用"False"而不是"Carousel",它不会自动播放.

我正在try 用按钮将"Carousel"替换为"False",这样用户就可以暂停自动播放模式:

这是我的HTML码:

<div class="container">
      <!-- WHEN data-bs-ride="carousel" it auto-plays, when data-bs-ride="false" it stops -->
      <div #carousel id="carousel" class="carousel slide" data-bs-ride="carousel" data-bs-pause="false">
        <div class="carousel-indicators">
          <button type="button" data-bs-target="#carousel" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
          <button type="button" data-bs-target="#carousel" data-bs-slide-to="1" aria-label="Slide 2"></button>
          <button type="button" data-bs-target="#carousel" data-bs-slide-to="2" aria-label="Slide 3"></button>
        </div>
        
        <div class="carousel-inner">

          <div *ngFor="let item of carouselItemsHorizontal" class="carousel-item {{item.activeClass}}" data-bs-interval="3000">
            <img src="{{item.imageSrc}}" class="carouselImage d-flex justify-content-center" alt="{{item.imageAlt}}">
              <div class="carousel-caption d-none d-md-block">
                <button routerLink="about" class="btn">Learn more</button>
              </div>
          </div>
          
        </div>
        
        <div class="carouselPlayPause">
          <svg (click)="playClick()" *ngIf="isPlaying" xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" class="bi bi-play playPauseColor" viewBox="0 0 16 16">
            <path d="M10.804 8 5 4.633v6.734zm.792-.696a.802.802 0 0 1 0 1.392l-6.363 3.692C4.713 12.69 4 12.345 4 11.692V4.308c0-.653.713-.998 1.233-.696z"/>
            <path d="M14 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2z"/>
          </svg>

          <svg (click)="pauseClick()" *ngIf="!isPlaying" xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" class="bi bi-pause playPauseColor" viewBox="0 0 16 16">
            <path d="M6 3.5a.5.5 0 0 1 .5.5v8a.5.5 0 0 1-1 0V4a.5.5 0 0 1 .5-.5m4 0a.5.5 0 0 1 .5.5v8a.5.5 0 0 1-1 0V4a.5.5 0 0 1 .5-.5"/>
            <path d="M14 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2z"/>
          </svg>
        </div>
        
      </div>
    </div>

这是我的Typescript 课程:

export class HomeCarouselComponent {

  dataRide:string = 'carousel';
  isPlaying:boolean = false;

  playClick():void {
    this.dataRide = 'carousel';
    this.isPlaying = !this.isPlaying;
  }

  pauseClick():void {
    this.dataRide = 'false';
    this.isPlaying = !this.isPlaying;
  }

  carouselItemsHorizontal:CarouselItems[] = [
    {
      imageSrc: '../../assets/images/home-images/carousel/main-artboard.png',
      imageAlt: 'Welcome to "Learning Angular" Application banner.',
      activeClass: 'active'
    },
    {
      imageSrc: '../../assets/images/home-images/carousel/sections-artboard.png',
      imageAlt: 'sections',
    },
    {
      imageSrc: '../../assets/images/home-images/carousel/projects-artboard.png',
      imageAlt: 'projects',
    },
  ]
}

顶部还有一个界面,用于项目形状:

interface CarouselItems {
  imageSrc:string;
  imageAlt:string;
  activeClass?:string;
}

我try 了这个[data-bs-ride]="dataRide",但给出了这个错误: 无法绑定到‘data-bs-ride’,因为它不是‘div’的已知属性.ngtsc(-998002)

我也try 了这个[attr.data-bs-ride]="dataRide",没有错误,但按钮不暂停/播放 carousel .

也不能使用字符串内插.

有什么好的建议吗?

非常提前感谢您!

[data-bs-ride]="dataRide"表示错误:ngtsc(-998002)

[attr.data-bs-ride]="dataRide",没有错误,但按钮不暂停/播放转盘.

推荐答案

不,这是行不通的,因为您不能暂停/启动具有HTML元素属性的Carousel实例.

取而代之的是,用JavaScript控制它.

只需在组件中获取Carousel实例,然后在相应的组件方法中暂停或滑动.pause and .cycle methods,而不是更改数据属性.

试试这个:

// get element and instantiate carousel

@ViewChild('carousel') carouselEl!:ElementRef;
carousel!:Carousel;

ngAfterViewInit() {
    this.carousel = Carousel.getOrCreateInstance(this.carouselEl.nativeElement);
} 

现在只需在您的方法中控制实例:

// pause/play

playClick():void {
this.carousel.cycle();
this.isPlaying = !this.isPlaying;
}

pauseClick():void {
this.carousel.pause();
this.isPlaying = !this.isPlaying;
}

Stackblitz demo

Angular相关问答推荐

使用多个管道时Angel 17空数据

Angular:将服务注入非独立组件导致应用程序中断

无效的ICU消息.缺少';}';|Angular material 拖放(&A)

如何确保[共享]组件是真正的哑组件?(Angular )

截获火灾前调用公共接口

无法在Mat-SideNav中绑定模式

RxJS轮询+使用退避策略重试

出错后可观察到的重用

npm install命令在基本Angular 应用程序的天蓝色构建管道中失败

Moment.js 和 Angular 库

Angular:组件的内容投影访问数据

console.log 返回 api 数据但 ERROR TypeError: Cannot read properties of undefined reading 'name'

Angular模板错误:Property does not exist on type component

为什么 HttpParams 在 Angular 4.3 中的多行中不起作用

如何在不刷新整页的情况下更新组件

Angular 7 测试:NullInjectorError:No provider for ActivatedRoute

带有 Angular 的 Font Awesome 5

Angular7 中的 CORS 策略已阻止源http://localhost:4200

找不到模块angular2/core

路由 getCurrentNavigation 始终返回 null