Sergiti
Sergiti

Reputation: 151

BxSlider with Thumbnails - Thumbnails dont slide along with the slider

I'm trying to fix a thumbnail BxSlider to work properly. I'm struggling with those:

1) When clicking on a thumbnail image the thumbnail slider has to slide along with the main slider.

2) And also making the main slider dragable. In order to change slides with the mouse.

3) The thumbnail arrows dont move the slides.

I've made an JSFIDDLE for you to see

And this is my my js code:

$(function() {

  var initThumbnailsSlider = function(object) {

    var $bxSlider = $(object);

    if ($bxSlider.length < 1) {
      return;
    }

    $bxSlider.bxSlider({
      controls: false,
      pagerCustom: '#bx-pager',
      easing: 'easeInOutQuint',
      infiniteLoop: true,
      speed: 500
    });

    $('.bx-custom-pager').bxSlider({
      mode: 'horizontal',
      maxSlides: 4,
      minSlides: 2,
      slideWidth: 156,
      slideMargin: 25,
      easing: 'easeInOutQuint',
      controls: true,
      nextText: "<i class='icm icm-Arrow_right'></i>",
      prevText: "<i class='icm icm-Arrow_left'></i>",
      pager: false,
      moveSlides: 1,
      infiniteLoop: false,
      speed: 500,
      onSlideBefore: bxMove

    });

    function bxMove($ele, from, to) {
      var idx = parseInt(to, 10) - 1;
      bx.goToSlide(idx);
    }

  };


  // execute the function
  initThumbnailsSlider('[data-bx-slider]');
});

Thanks a lot.

============================================

I've made a little search and i updated my JSFIDDLE

And I changed that part of my js code:

But sometimes it freezes :/

  $bxPager.bxSlider({
      mode: 'horizontal',
      maxSlides: 4,
      minSlides: 2,
      slideWidth: 156,
      slideMargin: 25,
      easing: 'easeInOutQuint',
      controls: true,
      pager: false,
      moveSlides: 1,
      speed: 500,
      onSlideBefore: bxMove

    });


    function bxMove($ele, from, to) {
      var idx = parseInt(to, 10) - 1;
      $bxSlider.goToSlide(idx);
    }

Upvotes: 0

Views: 6786

Answers (2)

zer00ne
zer00ne

Reputation: 44086

UPDATE

Since the original code is closer to what you wanted, but IMO not as good as the newer version, I figured I'll add it here:

  • Every click on an arrow advances both sliders in the same direction.
    • Every 6 clicks in one direction will cause the top slider to return to the same slide as the second slider's middle slide.
    • A click on one of the second slider's slide will make the first slider jump to the corresponding slide.

For some reason, the stack snippet is having DNS issues, so take a look at the

PLUNKER

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>SO35203571-38778710</title>
  <link rel="stylesheet" href="http://cdn.jsdelivr.net/bxslider/4.2.5/jquery.bxslider.css" />
  <style>
    #bx-pager {
      left: 25px;
    }
    .bx-wrapper a.active {
      border: 2px solid red;
    }
    .bx-controls-direction a {
      top: -100% !important;
    }
  }
  </style>
</head>

<body>
  <ul class="bxslider">
    <li>
      <img src="http://bxslider.com/images/730_200/hill_trees.jpg" />
    </li>
    <li>
      <img src="http://bxslider.com/images/730_200/me_trees.jpg" />
    </li>
    <li>
      <img src="http://bxslider.com/images/730_200/houses.jpg" />
    </li>
    <li>
      <img src="http://bxslider.com/images/730_200/tree_root.jpg" />
    </li>
    <li>
      <img src="http://bxslider.com/images/730_200/hill_fence.jpg" />
    </li>
    <li>
      <img src="http://bxslider.com/images/730_200/trees.jpg" />
    </li>
  </ul>
  <div id="bx-pager">
    <a data-slide-index="0" href="">
      <img src="http://bxslider.com/images/730_200/hill_trees.jpg" width="100" />
    </a>
    <a data-slide-index="1" href="">
      <img src="http://bxslider.com/images/730_200/me_trees.jpg" width="100" />
    </a>
    <a data-slide-index="2" href="">
      <img src="http://bxslider.com/images/730_200/houses.jpg" width="100" />
    </a>
    <a data-slide-index="3" href="">
      <img src="http://bxslider.com/images/730_200/tree_root.jpg" width="100" />
    </a>
    <a data-slide-index="4" href="">
      <img src="http://bxslider.com/images/730_200/hill_fence.jpg" width="100" />
    </a>
    <a data-slide-index="5" href="">
      <img src="http://bxslider.com/images/730_200/trees.jpg" width="100" />
    </a>
  </div>
  <script src="http://cdn.jsdelivr.net/jquery/2.2.0/jquery.min.js"></script>
  <script src="http://cdn.jsdelivr.net/bxslider/4.2.5/jquery.bxslider.min.js"></script>
  <script>
    $(function() {

      var bx = $('.bxslider').bxSlider({
        pagerCustom: '#bx-pager',
        controls: false
      });

      var cx = $('#bx-pager').bxSlider({
        mode: 'horizontal',
        maxSlides: 3,
        minSlides: 3,
        moveSlides: 1,
        slideWidth: 275,
        slideMargin: 40,
        pager: false,
        onSlideBefore: bxMove
      });

      function bxMove($ele, from, to) {
        var idx = parseInt(to, 10) - 1;
        bx.goToSlide(idx);
      }

    });
  </script>
</body>

</html>

When I wrote that, my intentions was to sync both sliders, but what I didn't know back then is that when using a carousel and using a variable range on min/maxSlides option gets really messy. bxSlider will clone slides in order to cover any inconsistencies like having an odd number of slides on an infiniteLoop while a resize occurs. That's a ton of calculations and memory, hence bxSlider freezing up, or ending up with slides only clearing halfway past the edge are common occurrences.

I've refactored it then added your Pokemon theme and Bootstrap to my original Fiddle. There are a number of changes but I'll try to keep it brief:

  1. Using the advanced easing options like: easing:'ease-in-out' requires:

    • useCSS: true (default)
    • jQuery Easing script loaded. So this should be added after the jquery.bxslider.min.js <script> tag: <script src="https://cdn.jsdelivr.net/jquery.easing/1.3/jquery.easing.1.3.min.js"></script>
    • Btw 'easeInOutQuint' isn't a value available with easing option, it's now `easing:'ease-in-out'. I have no idea why I added it in the first place.
  2. The thumbnail slider .bx-pager is now immobilized for 2 good reasons:

    • The design of thumbnail navigation (or any navigation for that matter), should be presented in it's full capacity.
    • The extra coordination involved in syncing a slider that presents one slide (i.e. .bxslider) and a slider that presents 4 slides (i.e. .bx-pager) becomes problematic.
  3. instantiate bxSlider in an expression:

    • var bx = $('.bxslider').bxSlider();
      • This makes using methods easier:
    • bx.goToSlide()
  4. Removed all controls since .bx-pager is more than sufficient.

  5. Tricked out thumbnails:

    • Used PNGs with a transparent background.
    • Added a simple function that toggles a class .on to the active thumbnail (i.e. .imgThumb.on}
    • This .on class employs transform, position, z-index, and transition CSS properties to animate.
    • If you resize the width smaller, you'll see the thumbnails layer on top of each other. That's a nice effect possible by using transparent background and avoiding cropping. So the background-size: cover which crops images is changed to background-size: contain which proportionally stretches the image to an element's edges without cropping.


Here's the demo. Good luck, sir.

FIDDLE

Upvotes: 1

Albert Israel
Albert Israel

Reputation: 585

I've used BxSlider before and haven't tried the slider you want to achieve. However, If you won't mind, I suggest that you try Slick. Below is a sample code that perfectly achieve what you want for your slide with lesser blocks of codes.

$('.product-slider').slick({
  slidesToShow: 1,
  slidesToScroll: 1,
  arrows: false,
  fade: true,
  asNavFor: '.custom-pager'
});
$('.custom-pager').slick({
  slidesToShow: 3,
  slidesToScroll: 1,
  asNavFor: '.product-slider',
  dots: false,
  centerMode: true,
  focusOnSelect: true
});
.custom-pager .img-container {
  width: 167px;
  height: 165px;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  overflow: hidden;
}

.product-slider .img-container {
  height: 525px;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  overflow: hidden;
}

.slide a{
  cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://cdn.jsdelivr.net/jquery.slick/1.6.0/slick.css" rel="stylesheet"/>
<script src="http://cdn.jsdelivr.net/jquery.slick/1.6.0/slick.min.js"></script>
<ul class="bxslider product-slider">

                  <!-- li.item -->
                  <li class="slide">
                    <div class="img-container" style="background-color: #f3f3f3; background-image: url('http://vignette4.wikia.nocookie.net/devilmaou/images/9/92/Giratina.png/revision/latest?cb=20140413190250')">
                    </div>
                  </li>
                  <!-- li : end -->

                  <!-- li.item -->
                  <li class="slide">
                    <div class="img-container" style="background-color: #f8f8f8; background-image: url('http://2.bp.blogspot.com/--gORRn5tL04/UDhREh-LLAI/AAAAAAAASA8/RN4gNJDMUm4/s1600/245Suicune+pokemon+gold+and+silver+artwork.png')">
                    </div>
                  </li>
                  <!-- li : end -->

                  <!-- li.item -->
                  <li class="slide">
                    <div class="img-container" style="background-color: #f3f3f3; background-image: url('http://vignette3.wikia.nocookie.net/pokemon/images/5/5c/486Regigigas_Pokemon_Ranger_Guardian_Signs.png/revision/latest?cb=20150114033117')">
                    </div>
                  </li>
                  <!-- li :end -->

                  <!-- li.item -->
                  <li class="slide">
                    <div class="img-container" style="background-color: #f3f3f3; background-image: url('http://www.legendarypokemon.net/images/xy/megavenusaur.jpg')">
                    </div>
                  </li>
                  <!-- li :end -->
</ul>
 <ul id="bx-pager" class="custom-pager">

                  <!-- li.item -->
                  <li class="slide">
                    <a class="block" data-slide-index="0">
                      <div class="img-container" style="background-color: #f3f3f3; background-image: url('http://vignette4.wikia.nocookie.net/devilmaou/images/9/92/Giratina.png/revision/latest?cb=20140413190250')">
                      </div>
                    </a>
                  </li>
                  <!-- li : end -->

                  <!-- li.item -->
                  <li class="slide">
                    <a class="block" data-slide-index="1">
                      <div class="img-container" style="background-color: #f8f8f8;  background-image: url('http://2.bp.blogspot.com/--gORRn5tL04/UDhREh-LLAI/AAAAAAAASA8/RN4gNJDMUm4/s1600/245Suicune+pokemon+gold+and+silver+artwork.png')">
                      </div>
                    </a>
                  </li>
                  <!-- li : end -->

                  <!-- li.item -->
                  <li class="slide">
                    <a class="block" data-slide-index="2">
                      <div class="img-container" style="background-color: #f3f3f3; background-image: url('http://vignette3.wikia.nocookie.net/pokemon/images/5/5c/486Regigigas_Pokemon_Ranger_Guardian_Signs.png/revision/latest?cb=20150114033117')">
                      </div>
                    </a>
                  </li>
                  <!-- li : end -->


                  <!-- li.item -->
                  <li class="slide">
                    <a class="block" data-slide-index="3">
                      <div class="img-container" style="background-color: #f3f3f3; background-image: url('http://www.legendarypokemon.net/images/xy/megavenusaur.jpg')">
                      </div> 
                    </a>
                  </li>
                  <!-- li : end -->

                </ul>

Upvotes: 1

Related Questions