3

Hi how can I get the current slide no when I click next and previous button in 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());
Jędrzej Chałubek
  • 1,410
  • 1
  • 16
  • 29
Amila Iddamalgoda
  • 4,166
  • 11
  • 46
  • 85

7 Answers7

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

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

  glide.mount();
Gilbert lucas
  • 519
  • 7
  • 22
2

For some reason, the function carousel.current() is not working.

You can use the code's callback and events instead. Example:

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

Also, carousel.index() works too!

khakiout
  • 2,372
  • 25
  • 32
1

Not sure why, but documentation about accessing API is missing. I'll fix that.

You need to access api via .data('glide_api'), before making api calls.

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

console.log(carousel.current());

Thanks for using plugin!

Jędrzej Chałubek
  • 1,410
  • 1
  • 16
  • 29
0

It works for me:

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

});
0

You can access the property index on your glide instance.

For example:

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 );
rhys_stubbs
  • 538
  • 6
  • 16
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();
          }
 
Rajaraman
  • 21
  • 4
-1
const glide = new Glide('.glide');

glide.on("run", () => {
   console.log(slider.index);
});
Ali Klein
  • 1,811
  • 13
  • 13