简体   繁体   English

Instagram 故事进度条

[英]Instagram Story Progress Bar

I'm building something similar to the instagram story function with HTML, TailwindCSS and JavaScript. I have 6 different slides where I'm navigating with touch taps with hammerJS.我正在构建类似于 instagram 故事 function 和 HTML、TailwindCSS 和 JavaScript 的东西。我有 6 个不同的幻灯片,我在其中使用 hammerJS 的触摸点击进行导航。 At the top I have 6 progression bars which should load while the user is viewing the current slide.在顶部我有 6 个进度条,当用户正在查看当前幻灯片时应该加载这些进度条。 I'm doing this with animeJS.我正在用 animeJS 做这个。 The carousel is set up with swiperJS.旋转木马是用 swiperJS 设置的。 My problem is, that I'm not able to load the progress bar of the current slide with animeJS.我的问题是,我无法使用 animeJS 加载当前幻灯片的进度条。 Maybe somebody can help me?也许有人可以帮助我?

HTML: HTML:

<div class="z-50 absolute fixed top-4 left-4 grid grid-cols-6 gap-4 max-w-md">
        <div class="w-10 h-1 bg-slate-200/50 rounded-full">
          <div id="progressSlide1" style="width: 0%;"
            class="h-full opacity-100 text-center text-xs text-white bg-white rounded-full"></div>
        </div>
        <div class="w-10 h-1 bg-slate-200/50 rounded-full">
          <div id="progressSlide2" style="width: 0%;"
            class="h-full opacity-100 text-center text-xs text-white bg-white rounded-full"></div>
        </div>
        <div class="w-10 h-1 bg-slate-200/50 rounded-full">
          <div id="progressSlide3" style="width: 0%;"
            class="h-full opacity-100 text-center text-xs text-white bg-white rounded-full"></div>
        </div>
        <div class="w-10 h-1 bg-slate-200/50 rounded-full">
          <div id="progressSlide4" style="width: 0%;"
            class="h-full opacity-100 text-center text-xs text-white bg-white rounded-full"></div>
        </div>
        <div class="w-10 h-1 bg-slate-200/50 rounded-full">
          <div id="progressSlide5" style="width: 0%;"
            class="h-full opacity-100 text-center text-xs text-white bg-white rounded-full"></div>
        </div>
        <div class="w-10 h-1 bg-slate-200/50 rounded-full">
          <div id="progressSlide6" style="width: 0%;"
            class="h-full opacity-100 text-center text-xs text-white bg-white rounded-full"></div>
        </div>
      </div>

The swiper carousel:刷卡轮播:

let swiper = new Swiper(".mySwiper", {
      speed: 0,
      longSwipes: false,
      allowTouchMove: false,
    });

Make the progress bar full of the previous screen (like if you skip through the story):让进度条充满前一个屏幕(就像你跳过故事一样):

swiper.on('slideNextTransitionStart', function () {
      for (let i = 0; i < swiper.activeIndex; i++) {
        $(`#progressSlide${i + 1}`).css('width', '100%');
      }
    });

    swiper.on('slidePrevTransitionStart', function () {
      for (let i = 6; i > swiper.activeIndex; i--) {
        $(`#progressSlide${i}`).css('width', '0%');
      }
    });

Touch navigation with hammerJS:使用 hammerJS 进行触摸导航:

let hammertime = new Hammer(document.querySelector('#swiper'));

    hammertime.on('tap', (e) => {
      if (e.center.x > window.innerWidth / 2) {
        swiper.slideNext();
      } else {
        swiper.slidePrev()
      }
    });

AnimeJS timeline: AnimeJS 时间线:

let timeline = anime.timeline({
      targets: `#progressSlide${swiper.activeIndex}`,
      width: "100%",
      autoplay: true,
      duration: 500,
      easing: 'linear',
      loop: false
    })

I want to load the current screen with anime JS like that:我想像这样用动漫 JS 加载当前屏幕:

swiper.on('slideChange', function () {
      timeline.pause();
      timeline.restart();
      timeline.add({
        targets: `#progressSlide${swiper.activeIndex}`,
        width: "100%"
      })
      timeline.play();
    });

but it's not working correctly.但它不能正常工作。 Every time it starts from the beginning, means after reaching screen 3 it starts with the progression animation for screen 1, 2 and 3. But I want it only for screen 3 and the current screens are already full loaded.每次从头开始,意味着在到达屏幕 3 后,它从屏幕 1、2 和 3 的进度 animation 开始。但我只想要屏幕 3,并且当前屏幕已经满载。

Found the solution:找到解决方案:

Create a anime function:创建动画 function:

function animationProcess() {
      anime({
        targets: `#progressSlide${swiper.activeIndex + 1}`,
        width: '100',
        easing: 'linear',
        duration: 5000,
        complete: function (anim) {
          swiper.slideNext();
        }
      });
    }

And add the.remove function and the animation function in the swiper update functions:并在swiper更新函数中添加.remove function和animation function:

swiper.on('slideNextTransitionStart', function () {
      for (let i = 0; i < swiper.activeIndex; i++) {
        $(`#progressSlide${i + 1}`).css('width', '100%');
      };
      anime.remove(`#progressSlide${swiper.activeIndex}`);
      animationProcess();
    });

    swiper.on('slidePrevTransitionStart', function () {
      for (let i = 6; i > swiper.activeIndex; i--) {
        $(`#progressSlide${i}`).css('width', '0%');
      };
      anime.remove(`#progressSlide${swiper.activeIndex + 2}`);
      animationProcess();
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM