bx-slider与图像一起滑动字幕外

时间:2016-02-18 15:59:54

标签: javascript jquery html bxslider

我试图弄清楚如何在bx-slider类之外滑动自定义标题。我只找到类似于here的答案,它使用了图像属性,但我的问题是我希望能够将图片与div中的图像一起滑动,与bx滑块分开,如果这样&# 39;可能。

到目前为止,我有这个:

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 个答案:

答案 0 :(得分:1)

听起来你想要的这个字幕框与滑块具有相同的功能。我做了2个bxSliders:

  • .bxslider修改如下:
    • var bx
    • 引用
    • 已删除onSliderLoad()回拨
    • onSlideBefore()回调更改为新功能syncTextSlider
  • #slider-text修改如下:
    • 实例化为bxSlider
    • var cx
    • 引用
    • 注意:controls: false
  • 定义了一个新函数syncTextSlider
    • .bxslider移至新幻灯片之前,系统会调用syncSliderText
    • 此函数然后在#slider-text
    • 上使用bxSlider API方法goToSlide()
  • 由于#slider-text的移动取决于.bxslider的移动,我删除了#slider-text的控件。
  • 两个滑块之间的样式.bxslider控件,如果您希望它们位于原始位置,只需删除位于<style>内的<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>