Reinitialize Slick js after successful ajax call

前端 未结 11 1611
傲寒
傲寒 2021-02-01 16:52

I am using Slick for a carousel implementation and everything works fine when the pages loads.What I am trying to achieve is that when i make an Ajax call to retrieve new data I

相关标签:
11条回答
  • 2021-02-01 17:12

    The best way is you should destroy the slick slider after reinitializing it.

    function slickCarousel() {
      $('.skills_section').slick({
        infinite: true,
        slidesToShow: 3,
        slidesToScroll: 1
      });
    }
    function destroyCarousel() {
      if ($('.skills_section').hasClass('slick-initialized')) {
        $('.skills_section').slick('destroy');
      }      
    }
    
    $.ajax({
      type: 'get',
      url: '/public/index',
      dataType: 'script',
      data: data_send,
      success: function() {
        destroyCarousel()
        slickCarousel();
      }
    });
    
    0 讨论(0)
  • 2021-02-01 17:12

    Here we go, guys! It helped me

    $('.slick-slider').not('.slick-initialized').slick({
       infinite: false,
       slidesToShow: 1,
       slidesToScroll: 1,
       dots: true,
       arrows: false,
       touchThreshold: 9
    });
    
    0 讨论(0)
  • 2021-02-01 17:13

    Note: I am not sure if this is correct but you can give it a try and see if this works.

    There is a unslick method which de-initializes the carousel, so you might try using that before re-initializing it.

    $.ajax({
        type: 'get',
        url: '/public/index',
        dataType: 'script',
        data: data_send,
        success: function() {
            $('.skills_section').unslick();  // destroy the previous instance
            slickCarousel();
          }
    });
    

    Hope this helps.

    0 讨论(0)
  • 2021-02-01 17:16
    $('#slick-slider').slick('refresh'); //Working for slick 1.8.1
    
    0 讨论(0)
  • 2021-02-01 17:17

    Try this code, it helped me!

    $('.slider-selector').not('.slick-initialized').slick({
      dots: true,
      arrows: false,
      setPosition: true
    });
    
    0 讨论(0)
  • 2021-02-01 17:20

    You should use the unslick method:

    function getSliderSettings(){
      return {
        infinite: true,
        slidesToShow: 3,
        slidesToScroll: 1
      }
    }
    
    $.ajax({
      type: 'get',
      url: '/public/index',
      dataType: 'script',
      data: data_send,
      success: function() {
        $('.skills_section').slick('unslick'); /* ONLY remove the classes and handlers added on initialize */
        $('.my-slide').remove(); /* Remove current slides elements, in case that you want to show new slides. */
        $('.skills_section').slick(getSliderSettings()); /* Initialize the slick again */
      }
    });
    
    0 讨论(0)
提交回复
热议问题