如何在滑翔.js中获取当前幻灯片

How to get the current slide no in Glide.js

本文关键字:获取 幻灯片 js      更新时间:2023-09-26
嗨,当我

在 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());
 const glide = new Glide('.glide');
  glide.on(['mount.after', 'run'], () => {
    const currentIndex = glide.index;
    console.log(currentIndex)
   });
  glide.mount();

由于某种原因,函数carousel.current()不起作用。

可以改用代码的回调和事件。例:

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

另外,carousel.index()也有效!

不知道为什么,但缺少有关访问 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());

感谢您使用插件!

它对我有用:

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()
});

您可以在滑翔实例上访问属性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 );

格利伯特答案有效

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();
          }
 

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