Public methods bxslider on multiple slideshows

可紊 提交于 2019-12-06 08:52:45

I had the same problem with multiple sliders on one page: public functions for controling the slides didn't work (the same way you do in your example).

The solution is to set controls=true and create unique 'prevSelector' and 'nextSelector' for each instance. This way the bxslider-instance have each unique prev en next-buttons (images or whatever you fill in there as text). The way I connect the bxslider to a unique div(id) is not required I think.

This implementation works for me with the childid as binding var for uniqueness:

Html (/php):

<div id="previousperiod_<?=$this->child->getId()?>"></div>
<div class="bxslider" id="bxslider_<?=$this->child->getId()?>" childid="<?=$this->child->getId()?>">
    <div>slide1</div>
    <div>slide2</div>
</div>
<div id="nextperiod_<?=$this->child->getId()?>"></div>

Javascript:

$("div.bxslider").each(function() {
    var childid = $(this).attr("childid");
    var sliderid = $(this).attr("id");
    $("div#"+sliderid).bxSlider({
        infiniteLoop: false,
        pager: false,
        controls: true,
        nextText: "<img src=\"img/icon/arrow_right.png\"/>",
        prevText: "<img src=\"img/icon/arrow_left.png\"/>",
        prevSelector: "#previousperiod_"+childid,
        nextSelector: "#nextperiod_"+childid,
        onSlideBefore: function($slideElement, oldIndex, newIndex) {
            //handle appearance prev- and nextSelectors
        }
    });
});

You can use the callback functions to handle appearance of the prev- and nextSelectors.

I had the same problem, but I solved with using each selector and object array. There are a "lot" of parent().parent().parent() but it works!

Notice, I am using custom next and prev links, an they must be outside slider container,.

Take a look at my script http://jsfiddle.net/33Jjr/1/

HTML

<div class="category-slider"> 
<a href="" class="prev">prev</a>  
<a href="" class="next">next</a> 
  <ul>
    <li><img src="http://placehold.it/350x150&text=FooBar1" /></li>
    <li><img src="http://placehold.it/350x150&text=FooBar2" /></li>
    <li><img src="http://placehold.it/350x150&text=FooBar3" /></li>
  </ul>
</div>

<div class="category-slider"> 
<a href="" class="prev">prev</a>  
<a href="" class="next">next</a> 
  <ul>
    <li><img src="http://placehold.it/350x150&text=FooBar7" /></li>
    <li><img src="http://placehold.it/350x150&text=FooBar8" /></li>
    <li><img src="http://placehold.it/350x150&text=FooBar9" /></li>
  </ul>
</div>

And Javascript

var swipes = []
$('.category-slider > ul').each(function (i, obj) {
swipes[i] = $(this).bxSlider({
    mode: 'horizontal',
    slideWidth: 200,
    minSlides: 3,
    maxSlides: 3,
    moveSlides: 1,  
    slideMargin: 0,
    pager: false,
    controls:false
})

$(this).parent().parent().parent().find('.prev').on('click', function (e) {
    e.preventDefault()
    swipes[i].goToPrevSlide()
})

$(this).parent().parent().parent().find('.next').on('click', function (e) {
    e.preventDefault()
    swipes[i].goToNextSlide()
  })
});

I couldn't find any problem with multiple sliders and public functions.

 $(document).ready(function(){
  var slideobj3=$('.slider1').bxSlider({
    mode: 'horizontal',
    slideWidth: 200,
    minSlides: 2,
    maxSlides: 3,
    slideMargin: 0,
    pager: false,
    controls:false
  });
  var slideobj2=$('.slider2').bxSlider({
    mode: 'horizontal',
    slideWidth: 200,
    minSlides: 2,
    maxSlides: 3,
    slideMargin: 0,
    pager: false,
    controls:false
  });
  var slideobj1=$('.slider3').bxSlider({
    mode: 'horizontal',
    slideWidth: 200,
    minSlides: 2,
    maxSlides: 3,
    slideMargin: 0,    
    pager: false,
    controls:false
  }); 
  $('#test').click(function (){
      slideobj1.goToNextSlide()
      slideobj2.goToPrevSlide()
      slideobj3.goToNextSlide()

    });
});

My code is working well.. bxslider with multiple rows.

I hope, jquery.bxslider.min.js will automatically handle the above process, then why should write separate script for this? and bxslider script working perfectly.

I had notice that this issue only appears if my 'mode' was set to 'horizontal' or 'vertical'. For 'fade' mode it works without any problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!