Reinitialize Slick js after successful ajax call

前端 未结 11 1612
傲寒
傲寒 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:25

    I had to unslick the carousel before the ajax call starts, but you can't do that until there is already a slick carousel. So, I set a variable to 0 and only run unslick after it changed

    var slide = 0
    if(slide>0)      
    $('#ui-id-1').slick('unslick');
    
            $.ajax({
                //do stuff, here
            },
            success: function( data ) {
    
                $('#ui-id-1').slick();
                slide++;
    
            }
    
    0 讨论(0)
  • 2021-02-01 17:27

    I was facing an issue where Slick carousel wasn't refreshing on new data, it was appending new slides to previous ones, I found an answer which solved my problem, it's very simple.

    try unslick, then assign your new data which is being rendered inside slick carousel, and then initialize slick again. these were the steps for me:

    jQuery('.class-or-#id').slick('unslick');
    myData = my-new-data;
    jQuery('.class-or-#id').slick({slick options});
    

    Note: check slick website for syntax just in case. also make sure you are not using unslick before slick is even initialized, what that means is simply initialize (like this jquery('.my-class').slick({options}); the first ajax call and once it is initialized then follow above steps, you may wanna use if else

    0 讨论(0)
  • 2021-02-01 17:27

    After calling an request, set timeout to initialize slick slider.

    var options = {
        arrows: false,
        slidesToShow: 1,
        variableWidth: true,
        centerPadding: '10px'
    }
    
    $.ajax({
        type: "GET",
        url: review_url+"?page="+page,
        success: function(result){
            setTimeout(function () {
                $(".reviews-page-carousel").slick(options)
            }, 500);
        }
    })
    

    Do not initialize slick slider at start. Just initialize after an AJAX with timeout. That should work for you.

    0 讨论(0)
  • 2021-02-01 17:29

    The best way would be to use the unslick setting or function(depending on your version of slick) as stated in the other answers but that did not work for me. I'm getting some errors from slick that seem to be related to this.

    What did work for now, however, is removing the slick-initialized and slick-slider classes from the container before reinitializing slick, like so:

    function slickCarousel() {
        $('.skills_section').removeClass("slick-initialized slick-slider");
        $('.skills_section').slick({
            infinite: true,
            slidesToShow: 3,
            slidesToScroll: 1
        });
    }
    

    Removing the classes doesn't seem to initiate the destroy event(not tested but makes sense) but does cause the later slick() call to behave properly so as long as you don't have any triggers on destroy, you should be good.

    0 讨论(0)
  • 2021-02-01 17:33

    This should work.

    $.ajax({
        type: 'get',
        url: '/public/index',
        dataType: 'script',
        data: data_send,
        success: function() {
            $('.skills_section').slick('reinit');
          }
    });
    
    0 讨论(0)
提交回复
热议问题