Skip to content

How to access Splide instances from within another function #502

Answered by NaotoshiFujita
StudioAl asked this question in Q&A
Discussion options

You must be logged in to vote

I guess you are struggling to relate elements observed by IntersectionObserver to Splide instances. The culprits that make your code much complex are jquery and iteration over the target elements twice. One solution is processing them in a single loop:

Array.from( document.querySelectorAll( '.section.carousel' ) ).forEach( elm => {
  const splide   = new Splide( elm, { autoplay: 'pause' } ).mount();
  const Autoplay = splide.Components.Autoplay;
  const observer = new IntersectionObserver( ( [ entry ] ) => {
    entry.isIntersecting ? Autoplay.play() : Autoplay.pause();
  }, { threshold: 0.6 } );
  observer.observe( elm );
} );

Note that you are using a string as an array key, but you sho…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@StudioAl
Comment options

@NaotoshiFujita
Comment options

Answer selected by StudioAl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants