如何在Glide.js中获取当前幻灯片编号

3

嗨,当我点击GLide.js的下一个和上一个按钮时,我该如何获取当前幻灯片编号?http://glide.jedrzejchalubek.com/docs.html#intro

var carousel = $('#Carousel').glide({
                type: 'carousel',
                startAt: 1,
                touchDistance: 2,
          afterInit:function(){console.log("paintSlider")},
          autoplay: 0
              });
console.log(carousel.current());
7个回答

3
 const glide = new Glide('.glide');

  glide.on(['mount.after', 'run'], () => {
    const currentIndex = glide.index;
    console.log(currentIndex)
   });

  glide.mount();

2

由于某些原因,函数carousel.current()无法正常工作。

您可以使用代码的回调函数和事件代替。 例如:

var carousel = $('#Carousel').glide({
            type: 'carousel',
            ...
            afterTransition : function(event) {
                console.log(event.index); // the current slide number
            } 
        });

另外,carousel.index() 也可以使用!


1

不确定为什么,但是关于访问API的文档缺失。我会修复这个问题。

在进行api调用之前,您需要通过.data('glide_api')来访问api。

var carousel = $('#Carousel').glide({
      type: 'carousel',
      startAt: 1,
      touchDistance: 2,
      afterInit:function(){console.log("paintSlider")},
      autoplay: 0
}).data('glide_api');

console.log(carousel.current());

感谢使用插件!

0

Glibert answer works

var stGlide = new Glide(`.heroglide-${article.id}`, {
          type: 'carousel',
          animationDuration: 500,
          startAt: 1,
          perView: 1,
          peek: {
              before: 50,
              after: 50
          },
        //   afterTransition : function(event) {
        //     console.log("========transition",event.index); // the current slide number
        // } 
          }); 
          try{
         
          stGlide.on(['mount.after', 'run'], () => {
            const currentIndex = stGlide.index;
            console.log("====>index==>",currentIndex)
           
           });

         
            stGlide.mount();
          }
 


0

你可以在 glide 实例上访问属性 index

例如:

import Glide, { Controls } from '@glidejs/glide/dist/glide.modular.esm';

var glider = new Glide( el, options );

glider.mount( {
  Controls,
} );

// You can get the current index using glider.index;
console.log( glider.index );

0

对我来说它有效:

jQuery(document).ready(function(){

    var glide = jQuery('.slider').glide({
        afterTransition : function() {
            console.log(glide.current());
        } 
    }).data('api_glide'); // data('api_glide') to get opportunity to use glide.current()

});

-1
const glide = new Glide('.glide');

glide.on("run", () => {
   console.log(slider.index);
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接