Is there a destroy method for FlexSlider
Asked Answered
A

3

6

I am trying to reinitialize FlexSlider with out page refresh when a new gallery listing is called asynchronously.

I would think the routine below would work but it doesn't. It seems like the first FlexSlider persists even though the new images are successfully loaded.

Is there a way to destroy, then rebuild the gallery?

Thanks

function flexInit() {
        $('.flexslider').flexslider({
            animation: "slide",
            controlsContainer: ".paginator",
            manualControls: 'a',
            after: function(slider){
                if(slider.atEnd == true)    {
                    // ??? slider.destroy;
                    galBuild();
                }
            }
        });
    }

    function galBuild() {
            $.getJSON("/gallery/next/"+galID, function (data) {
            var results = data.objects;
            var list = $(".flexslider ul.slides");
            var i = 0;
            $.each(results, function () {

                list.append('<li><p>' + results[i].title + '</p><img src="' + results[i].src + '"><p class="flex-caption">' + results[i++].caption + '</p></li>');

            });

            flexInit(); 

            });
    }

    galBuild();
Ahab answered 28/6, 2012 at 15:49 Comment(2)
from where "galID" argument passed in galBuild() function?Fiscus
Hi Kundan... galID is just a global gallery variable declared earlier on. Doesn't really have any bearing on reinitializing FlexSliderAhab
A
10

i'm using different approach i.e.

you've started one flexslider:


    $('#element).flexslider({
      animation: "slide",
      controlNav: false,
      directionNav: true
    });

when I want to change slides in previously created slider and restart it, id do following things:

  • creating temporaty div:

    $('#element).before('
        <div id="element_temp" class="flexslider"></div>
    ');
    
  • delete div with previously created slider

    $('#element).remove();
    
  • insert new list of slides into temporary div:

    var html = `
    <ul class='slides">
    <li><img src="link to image" /></li>
    <li><img src="link to image" /></li>
    <li><img src="link to image" /></li>
    </ul>`;
    $('#element_temp').html(html);
    
  • start flexslider on temp div

    $('#element_temp').flexslider({
      animation: "slide",
      controlNav: false,
      directionNav: true
    });
    
  • change div ID from element_temp to element

    $('#element_temp').attr('id','element');
    

and it works with multiple flexliders

Armond answered 4/12, 2012 at 10:56 Comment(2)
this was very helpful - I didn't need to change the id at the end though it was screwing the up the start point of my slider but I apprecaited the infoAksoyn
Thank you! It's unfortunate FlexSlider doesn't provide an easy way to 'reset' the slider. This solution seems to be working well though.Erbes
F
2

Rob i investigated this and find the solution

You need to modify your functions like this

function flexInit() {
    $('.flexslider').flexslider({
        animation: "slide",
        controlsContainer: ".paginator",
        manualControls: 'a',
        after: function(slider){
            if(slider.atEnd == true)    {
                slider.addSlide(galBuild());
            }
        }
    });
}


function galBuild() {
   $.getJSON("/gallery/next/"+galID, function (data) {
    var results = data.objects;
    var i = 0;
    $.each(results, function () {
       return ('<li><p>' + results[i].title + '</p><img src="' + results[i].src + '"><p class="flex-caption">' + results[i++].caption + '</p></li>');
    });

   });
}

flexInit();

Also you need to do some cosmetic changes in flexSlider.js file in slider.update function. right now its not checking the position variable if it comes undefined, so you will have to check this as well.

Fiscus answered 28/6, 2012 at 18:2 Comment(1)
Kundan... you're definitely taking me down the right road here. THanks. When the FlexSlider is called again though, the control area goes wild trying to get setup and I get a stack error $.flexslider.methods.controlNav.update $.flexslider.slider.update I will keep you updated on my progressAhab
C
2

The most easy way is to remove the flexscroll from element dataset

function flexInit() {
    $('.flexslider').flexslider({
        animation: "slide",
        controlsContainer: ".paginator",
        manualControls: 'a',
        after: function(slider){
            if(slider.atEnd == true)    {
                slider.addSlide(galBuild());
            }
        }
    });
}
//this remove flexslider form element dataset
$('.flexslider').removeData("flexslider");

and now you are free to call

flexInit();

and your flexslider will be recreated.

Carmencarmena answered 5/11, 2013 at 21:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.