bx-slider sliding outside captions along with the image

烈酒焚心 提交于 2019-12-23 22:02:25

问题


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
   }
});

回答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>


来源:https://stackoverflow.com/questions/35486338/bx-slider-sliding-outside-captions-along-with-the-image

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