Add or remove slides using jQuery FlexSlider

2019-04-07 04:28发布

Is it possible to add or remove slides in runtime using FlexSlider?

4条回答
戒情不戒烟
2楼-- · 2019-04-07 04:58

This is just what I saw after looking at this thread.

The slider and the carousel object can be instantiated and added to like this:

$('#slider').data('flexslider').addSlide("");

$('#carousel').data('flexslider').addSlide("");

The click on the carousel to scroll to the particular image doesn't work, but the scroll buttons on both work.

查看更多
老娘就宠你
3楼-- · 2019-04-07 05:00

The new version of FlexSlider 2 already supports this methods.

slider.addSlide(obj, pos) accepts two parameters, a string/jQuery object and an index. slider.removeSlide(obj) accepts one parameter, either an object to be removed, or an index.

查看更多
手持菜刀,她持情操
4楼-- · 2019-04-07 05:00

The actual implementation of FlexSlider doesn't support it.

If you modify the actual implementation to return the slider object, with this object you can stop the slider, remove the slide you want and then recreate the slider.

查看更多
虎瘦雄心在
5楼-- · 2019-04-07 05:06

After trying lots of different ideas, I got this solution to add or remove a new image or video in Flexslider dynamically and its working fine.

JQuery code:

$("#add2").change(function(event)
    {
          var fuData = document.getElementById('add2');
          var files = event.target.files;
           for(var i = 0; i< files.length; i++)
        {
            var file = files[i];
            var filename = file.name;
            var Extension =
filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
            if(Extension == 'png' || Extension == 'jpg' || Extension == 'jpeg' || Extension == 'svg'){
                var reader = new FileReader();
                reader.onload = function(e)
                {
                      var img = document.createElement("IMG");
                      img.src = e.target.result;

                      div = "<li><img src="+img.src+" /></li>";
                      $('.flexslider').data('flexslider').addSlide($(div));

                }
            }

            else if (Extension == 'mp4')
            {
                var reader = new FileReader();
                reader.onload = function(event){
                    var video = document.createElement("video");
                    video.src = event.target.result;

                    div = " <li><video src="+video.src+" width='100%' height='500' controls></video></li>";
                    $('.flexslider').data('flexslider').addSlide($(div));
                }
            }
            else
            {
              alert(filename+' '+'is not in supported format');
              $("#add2").val('');
            }
            reader.readAsDataURL(file);
        }
    });

function remove()
{
  var slider = $('.flexslider').data('flexslider');
  slider.removeSlide(slider.currentSlide);
}

HTML code:

<input type="file" id= "add2" multiple>
<button id="remove" onclick="remove()" value="Remove">Remove</button>

as per the code, with browse file, you can select multiple images and videos to add in Flexslider and with remove button, you can remove a current slide.I also added some validation so only image or video will be add in a slider. It will give an alert if you select any other extension. I created this code as per my requirement, you can customize it accordingly to your requirements.

查看更多
登录 后发表回答