Search code examples
javascriptjquerycsssliderbxslider

"bxslider jquery" change the slide mode at specified width


like the title i'm using bxslider at my website, actually i'm using vertical thumbnail slide now, but the problem is, it's look ugly when at mobile width, so i want to change it like normal slider with arrow. can you help me how to do that? here is the things i want: 1. at normal using vertical thumb slide 2. when the width triggered at specify width, the thumb is hide (maybe display none at css) 3. Do destroyslider for stopping the vertical thumb slider and change it to normal slider with arrow 4. when the width is normal again, it will switch again to vertical thumb slider

ok here is my jquery:

var mainImage;
$(window).load(function() {
    mainImage = $('.product-gallery').bxSlider({
        infiniteLoop: false,
        hideControlOnEnd: true,
        pager: false,
        controls: false,
        auto: false,
        minSlides: 1,
        maxSlides: 1,
        moveSlides: 1,
        mode: 'fade',
        adaptiveHeight: true
    });

    $('.product-gallery-thumbs ul').bxSlider({
        infiniteLoop: false,
        hideControlOnEnd: true,
        pager: false,
        auto: false,
        minSlides: 4,
        maxSlides: 4,
        moveSlides: 1,
        slideWidth: '100%',
        mode: "#{options['product_thumbnail_direction']}",
        slideMargin: 10,
        onSliderLoad: function(currentIndex) {
            $('.product-gallery-thumbs ul li').eq(0).addClass('active')
        }
    });

    $('.product-gallery-thumbs ul li').each(function() {
        $(this).click(function() {
            $(this).addClass('active').siblings().removeClass('active');
            mainImage.goToSlide($(this).index());
        });
    });
});

when do destroy :

mainImage.destroySlider();

here is normal slider with arrow:

mainImage.bxSlider({
    mode: 'fade',
    captions: true,
    slideWidth: 600
  });

UPDATE : I try to did this, when do destroy is work, but when switching to normal arrow slider it doesn't work. Hope you guys can help me fix this

$(document).ready(function(){
            setMaxWidth(767px);
            mainImage.destroySlider();

            function setMaxWidth(767px) {
              mainImage.bxSlider({
                mode: 'fade',
                captions: true,
                slideWidth: 600
              });
            })

Solution

  • bx.reloadSlider() Method

    The method destroySlider() returns bxSlider as it was initially so don't use it if you want to change bxSlider after a reload, instead use reloadSlider(). Besides the obvious (reloading bxSlider), you can load a different set of options. Note: There are 2 Object Literals:

    var cfgA = {...} and var cfgB = {...}

    Each of them has key/value pairs (or properties) that are from the bxSlider API. When calling .bxSlider(), simply load one of configurations:

    var bx = $('.bx').bxSlider(cfgA);

    onSliderResize Callback

    As far as slider plugins go, bxSlider isn't the most stable and it can get buggy as browsers update and bxSlider stagnates at v.4.2.12, but there's one thing bxSlider excels in is callbacks. I've written complex programs that are controlled by bxSlider callbacks because they are reliable and never break. In this demo onSliderResize call the function reloadBX(currentIndex). **Note: ** the absence of parenthesis on reloadBX():

    onSliderResize: reloadBX

    reloadBX(currentIndex)

    This is the heart of the Demo. For demonstration purposes I've bound the function to a convenient button. When testing the Demo, you should expect the following by either clicking the RELOAD button, or resizing bxSlider:

    • Toggling between cfgA and cfgB

    • Relocates to the position index bxSlider was on before the reload.

    • Shifts images to adjust at reload to avoid images being stuck halfway.


    Details commented in Demo

    Demo

    <!doctype html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <title>bxSlider Reload</title>
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.css">
      <style>
        img {
          margin: auto;
          display: block;
          max-width: 100%;
        }
      </style>
    </head>
    
    <body>
      <button class='reload'>RELOAD</button>
      <ul class='bx'>
        <li>
          <img src="https://i.imgur.com/DrEwPH0.jpg">
          <img src="https://i.imgur.com/MEPxbq4.jpg">
        </li>
        <li>
          <img src="https://i.imgur.com/6qGdA1e.gif">
        </li>
        <li>
          <img src='https://i.imgur.com/DsM6J8U.gif'>
        </li>
        <li>
          <img src="https://i.imgur.com/sbxyLKX.png">
        </li>
        <li>
          <img src="https://i.imgur.com/DheohWR.gif">
        </li>
      </ul>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.min.js"></script>
    
      <script>
        // Cfg flag
        var AB = 'A';
    
        // Index counter
        var idx = 0;
    
        // cfgA
        var cfgA = {
          mode: 'horizontal',
          slideWidth: 320,
          adaptiveHeight: true,
          onSliderResize: reloadBX
        };
    
        // cfgB
        var cfgB = {
          mode: 'fade',
          captions: true,
          slideWidth: 160,
          adaptiveHeight: true,
          onSliderResize: reloadBX
        };
    
        // Reference bxSlider instance
        var bx = $('.bx').bxSlider(cfgA);
    
        /*
        Resize Callback 
        */ // Called on resize event
        function reloadBX(current) {
    
          // Store current index
          idx = current;
    
          // Determine what configuration bx is in
          if (AB === "A") {
    
            /* if bx is in cfgA...
            || reload slider with cfgB
            */
            bx.reloadSlider(cfgB);
    
            // Shift all img to the left
            $('img').css('transform', 'translateX(-25%)');
    
            // Switch cfg flag to B
            AB = "B";
    
            // Otherwise...
          } else {
    
            // Reload slider with cfgA
            bx.reloadSlider(cfgA);
    
            // Shift all img to the right
            $('img').css('transform', 'translateX(0)');
    
            // Switch cfg flag to A
            AB = "A";
          }
    
          // Go back to the index before reload
          bx.goToSlide(idx);
        }
    
        // Added a reload button for convenient testing 
        $('.reload').click(function(e) {
    
          // Get the current index and store it
          idx = bx.getCurrentSlide();
    
          /* Need to use the .call() function so that the context
          || is changed to bxSlider
          */
          reloadBX.call(this, idx);
        });
      </script>
    </body>
    
    </html>