获取 Angular 中 Swiper 的自动播放时间剩余。

huangapple go评论65阅读模式
英文:

How to get swiper autoplayTimeLeft in Angular

问题

我想创建自己的分页,当自动播放时,希望有一个自动填充的进度条。

我看到Swiper确实有一个autoplayTimeLeft方法,但我不知道如何使用它。

这是我迄今为止尝试过的,但这个方法不会触发任何东西。

<swiper-container #swiperRef init="false" (autoplayTimeLeft)="setTimeLeft($event)">
</swiper-container>

我按照以下方式实例化了Swiper:as follow

英文:

I'd like to create my own pagination, with a bar filling himself while the autoplay is playing

获取 Angular 中 Swiper 的自动播放时间剩余。

I see that swiper do have an autoplayTimeLeft method, but I cannot figure out how to use it.

This is what I've tried so fare, but the method wont trigger anything.

&lt;swiper-container #swiperRef init=&quot;false&quot; (autoplayTimeLeft)=&quot;setTimeLeft($event)&quot;&gt;
&lt;/swiper-container&gt;

I'm instantiating the swiper as follow

答案1

得分: 2

你应该将它用作事件。然后,在 _initSwiper() 方法中,你可以执行以下操作:

percentage:number=0;
private _initSwiper() {
  const options: SwiperOptions = {...}

  const swiperEl = this._swiperRef.nativeElement
  Object.assign(swiperEl, options)

  swiperEl.initialize()

  if (this.swiper) this.swiper.currentBreakpoint = false // 修复断点
  this.swiper = this._swiperRef.nativeElement.swiper
  
  // 自动播放剩余时间事件
  this.swiper.on('autoplayTimeLeft',(swiper:Swiper, timeLeft:number, percentage:number)=>{
    // 在这里编写你需要的代码
    console.log(swiper, timeLeft, percentage);
    this.percentage=percentage;
  });
}

这里有一个类似于你想要实现的示例:示例链接

英文:

You should use it as an event. Then, in the _initSwiper() method, you can do the following:

percentage:number=0;
private _initSwiper() {
  const options: SwiperOptions = {...}

  const swiperEl = this._swiperRef.nativeElement
  Object.assign(swiperEl, options)

  swiperEl.initialize()

  if (this.swiper) this.swiper.currentBreakpoint = false // Breakpoint fixes
  this.swiper = this._swiperRef.nativeElement.swiper
  
  // Autoplay Time Left Event
  this.swiper.on(&#39;autoplayTimeLeft&#39;,(swiper:Swiper, timeLeft:number, percentage:number)=&gt;{
    // Here write the code you need
    console.log(swiper, timeLeft, percentage);
    this.percentage=percentage;
  });
}

Here's an example similar to what you want to achieve.

答案2

得分: 0

以下是翻译好的部分:

正如我在我的问题中所示的图像中所示,我需要知道活动索引,以便我能够使用CSS显示白线。

自动播放功能可悲地有一个小错误,由于它永远不会重置为0,因此无法获得正确的swiper.activeIndex

我不得不实现一个自定义功能,并希望与您分享。

percentage:number = 0
activeIndex = 0
defaultDelay = 500
slides = ['1', '2', '3']

private _initSwiper() {
  const options: SwiperOptions = {...}

  const swiperEl = this._swiperRef.nativeElement
  Object.assign(swiperEl, options)

  swiperEl.initialize()

  if (this.swiper) this.swiper.currentBreakpoint = false // Breakpoint fixes
  this.swiper = this._swiperRef.nativeElement.swiper
  
  // 自定义自动播放时间
  this.doAutoplay(this.defaultDelay, this.activeIndex)

  // 如果用户手动更改幻灯片
  this.swiper.on('slideChange', (swiper: Swiper) => {
    this.activeIndex = swiper.activeIndex
    this.doAutoplay(this.defaultDelay, this.activeIndex)
  })
}


doAutoplay(delay: number, index: number) {
  if (this.activeIndex !== index) return

  this.percentage = 100 - (delay / this.defaultDelay) * 100 + '%'

  if (delay <= 0) {
    setTimeout(() => {
      if (this.activeIndex + 1 === this.slides.length) this.swiper.slideTo(0)
      else this.swiper.slideTo(this.activeIndex + 1)
    }, 300)
    return
  }

  setTimeout(() => {
    this.doAutoplay(delay - 10, index)
  }, 10)
}
英文:

As I show in the image in my question, I've needed to know the active index for me to be able to show the white line with css.

The autoplay feature have sadly a little bug and it is impossible to get the correct swiper.activeIndex since it does never reset to 0.

I had to implement a custom feature and wished to share it with you.

percentage:number = 0
activeIndex = 0
defaultDelay = 500
slides = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;]

private _initSwiper() {
  const options: SwiperOptions = {...}

  const swiperEl = this._swiperRef.nativeElement
  Object.assign(swiperEl, options)

  swiperEl.initialize()

  if (this.swiper) this.swiper.currentBreakpoint = false // Breakpoint fixes
  this.swiper = this._swiperRef.nativeElement.swiper
  
  // Custom Autoplay Time
  this.doAutoplay(this.defaultDelay, this.activeIndex)

  // If the user manually change the slide
  this.swiper.on(&#39;slideChange&#39;, (swiper: Swiper) =&gt; {
    this.activeIndex = swiper.activeIndex
    this.doAutoplay(this.defaultDelay, this.activeIndex)
  })
}


doAutoplay(delay: number, index: number) {
  if (this.activeIndex !== index) return

  this.percentage = 100 - (delay / this.defaultDelay) * 100 + &#39;%&#39;

  if (delay &lt;= 0) {
    setTimeout(() =&gt; {
      if (this.activeIndex + 1 === this.slides.length) this.swiper.slideTo(0)
      else this.swiper.slideTo(this.activeIndex + 1)
    }, 300)
    return
  }

  setTimeout(() =&gt; {
    this.doAutoplay(delay - 10, index)
  }, 10)
}

huangapple
  • 本文由 发表于 2023年7月4日 23:49:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定