Search code examples
javascriptjquerycsssliderglidejs

On clicking element restart Glide.js


I have a lightbox with a glide slider in it. Glide.js is already initiated. I want on clicking my specified element to re-start the slider. When closing the lightbox I'd like to pause the slider. I can see that this is possible with the API but cant get it to work...

My slider setup

var slider_alt=$("#glide--alt").glide({
    type:"slideshow",
    autoplay:2500, 
    animationDuration:2000,
    hoverpause:false
}),
slider_api=slider_alt.data("glide_api");

My jQuery for trying to restart the slider

popUp_openButton.click(function(){
    slider_alt.jump(1);
    slider_alt.play();
});

popUp_closeButton.click(function(){ 
    slider_alt.pause();
}); 

Solution

  • Heres my final working code. On opening the modal window the slider is recalculated and starts at slide 1. On closing its stops clearing all objects and bindings.

    //Setting up slider
    var slider_alt=$("#glide--alt").glide({
        type:"slideshow",
        autoplay:2500, 
        animationDuration:2000,
        hoverpause:false
    }),
    slider_alt_api=slider_alt.data("glide_api");
    
    
    var popUp_openButton    = $('.popUp__button--open'),
    popUp_closeButton   = $('.popUp__button--close');
    
    popUp_openButton.click(function(){
        slider_alt_api.refresh();
        slider_alt_api.jump('=1');
    });
    
    popUp_closeButton.click(function(){ 
        slider_alt_api.destroy();
    });     
    
    $(document).keyup(function(e){
        if(e.keyCode === 27) {
            slider_alt_api.destroy();
        }  
    });