2

I'm trying to figure out how to slide custom captions outside of bx-slider class. I only found answers similar to this here, where it uses the image attribute, but my problem is I want to be able to slide a caption along with the image inside a div, separate from bx-slider, if that's possible.

So far I have this:

HTML:

<!--image slider desktop-->
<div id="hero">
   <ul class="bxslider">
      <li>
         <a href="www.google.com">
           <img src="http://placehold.it/350x150" class="slider-img-responsive">
         </a>
       </li>
       <li>
         <a href="www.google.com">
          <img src="http://placehold.it/350x150" class="slider-img-responsive">
         </a>
       </li>
    </ul>
</div>
<!-- slider text-->
<div class="mobile-homepage-header">
    <!-- TODO: slide text relative to image slider-->
    <ul id="slider-text" style="list-style-type:none;padding:0;">
        <li>
            <h1>Lorem ipsum 1</h1>
            <h2>Lorem ipsum dolor sit 1</h2>
            <a href="www.google.com">Button 1</a>
        </li>
        <li>
            <h1>Lorem ipsum 2.</h1>
            <h2>Lorem ipsum dolor sit 2</h2>
            <a href="www.google.com">Button 2</a>
        </li>
    </ul>
</div>

JQuery:

$('#hero .bxslider').bxSlider({
   auto: true,
   infiniteLoop: true,
   pager: false,
   controls: true,
   pause: 5000,
   onSliderLoad: function(currentIndex) {
       $('#slider-text li').html($('.bxslider li').eq(currentIndex)); // select the current index of the slider
   },
   onSlideBefore: function($slideElement, oldIndex, newIndex) {
       $('#slider-text li').html($slideElement); // slide text along with image
   }
});
Community
  • 1
  • 1
TheAmazingKnight
  • 2,442
  • 9
  • 49
  • 77

1 Answers1

1

It sounds like this caption box you want has the same functions as a slider. I made 2 bxSliders:

  • .bxslider is modified as follows:
    • Referenced by var bx
    • Removed onSliderLoad() callback
    • Changed onSlideBefore() callback to a new function syncTextSlider
  • #slider-text is modified as follows:
    • Instantiated to a bxSlider
    • Referenced by var cx
    • Note: controls: false
  • Defined a new function syncTextSlider
    • Before .bxslider moves to a new slide, syncSliderText is called.
    • This function then uses bxSlider API method goToSlide() on #slider-text
  • Since #slider-text's movements depend on .bxslider's movements I removed #slider-text's controls.
  • Styled .bxslider's controls in between both sliders, if you prefer them in the original position, just remove the style located in the <style> block inside the <head>.

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>35486338</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/bxslider/4.2.5/jquery.bxslider.css">
  <style>
    /* Remove style to relocate controls to original position */
    .bx-controls-direction a {
      top: 125% !important;
    }
  </style>
</head>

<body>
  <!--image slider desktop-->
  <div id="hero">
    <ul class="bxslider">
      <li>
        <a href="www.google.com">
          <img src="http://placehold.it/350x150/000/0ff/?text=1" class="slider-img-responsive">
        </a>
      </li>
      <li>
        <a href="www.google.com">
          <img src="http://placehold.it/350x150/000/fff/?text=2" class="slider-img-responsive">
        </a>
      </li>
      <li>
        <a href="www.google.com">
          <img src="http://placehold.it/350x150/000/e00/?text=3" class="slider-img-responsive">
        </a>
      </li>
      <li>
        <a href="www.google.com">
          <img src="http://placehold.it/350x150/000/f3f/?text=4" class="slider-img-responsive">
        </a>
      </li>
      <li>
        <a href="www.google.com">
          <img src="http://placehold.it/350x150/000/3f3/?text=5" class="slider-img-responsive">
        </a>
      </li>
    </ul>
  </div>
  <!-- slider text-->
  <div class="mobile-homepage-header">
    <ul id="slider-text">
      <li>
        <h1>Title 1</h1>
        <h2>Byline 1</h2>
        <a href="www.google.com">Button 1</a>
      </li>
      <li>
        <h1>Title 2</h1>
        <h2>Byline 2</h2>
        <a href="www.google.com">Button 2</a>
      </li>
      <li>
        <h1>Title 3</h1>
        <h2>Byline 3</h2>
        <a href="www.google.com">Button 3</a>
      </li>
      <li>
        <h1>Title 4</h1>
        <h2>Byline 4</h2>
        <a href="www.google.com">Button 4</a>
      </li>
      <li>
        <h1>Title 5</h1>
        <h2>Byline 5</h2>
        <a href="www.google.com">Button 5</a>
      </li>
    </ul>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/bxslider/4.2.5/jquery.bxslider.min.js"></script>
  <script>
    var bx = $('.bxslider').bxSlider({
      auto: true,
      infiniteLoop: true,
      pager: false,
      controls: true,
      pause: 5000,
      onSlideBefore: syncTextSlider
    });

    var cx = $("#slider-text").bxSlider({
      infiniteLoop: true,
      pager: false,
      controls: false
    });

    function syncTextSlider($ele, from, to) {
      cx.goToSlide(to);
    }
  </script>
</body>

</html>
zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • Thank you so much for your help! I noticed the last slide going back to the first slide would inverse direction with a left transition instead of going right as the image. A simple solution is to replace `goToSlide(to)` with `goToNextSlide(to)` for those of you who want to make the slider and caption stay in the same transitioned direction to each other (especially on the last to first slide). – TheAmazingKnight Feb 22 '16 at 16:48
  • No problem at all, sir. Thank you, for your different take on the subject, it is duly noted. It's the little additions that are steps to a better solution. :-) – zer00ne Feb 22 '16 at 18:25