同步flex滑块-flex-direction-nav(箭头)

时间:2018-10-02 06:34:45

标签: javascript jquery html flexslider

我正在使用flex-slider开发Slider。我想到了将两个flexsliders同步到一个的想法,就像处理Owl-carousel一样。当柔韧性字幕与图像进行动画处理时,我必须使用两个flexslider,因为我希望柔韧性字幕动画不同于图像动画。我已经在java.time.LocalDateTime的帮助下同步了它们 但是现在我陷入了两点:

1)我也希望箭头也同步。如您在我的代码中看到的,我将flex-direction-navs分组了。现在,我想在单击一个按钮时更改两个幻灯片,但这不起作用,因为它上面有层次。

2)我希望自定义定位按钮处于活动状态,具体取决于活动幻灯片。例如,我在第三张幻灯片上,我希望第三个定位锚标签处于活动状态,例如颜色变化等。

希望我清楚地解释了我的问题。如果您需要更多信息,请在下面评论。

这里是Answer链接。跟着代码。

ActionBar{
    background-color: rgba(255, 0, 0, 0.0);
}
jQuery(document).ready(function($) {

  $('#main-slider').flexslider({
    animation: "slide",
    slideToStart: 0,
    start: function(slider) {
      $('a.slide_thumb').click(function() {
        $('.flexslider').show();
        var slideTo = $(this).attr("rel") //Grab rel value from link;
        var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
        if (slider.currentSlide != slideToInt) {
          slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
        }
      });
    }

  });

  $('#secondary-slider').flexslider({
    animation: "slide",
    slideToStart: 0,
    start: function(slider) {
      $('a.slide_thumb').click(function() {
        $('.flexslider').show();
        var slideTo = $(this).attr("rel") //Grab rel value from link;
        var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
        if (slider.currentSlide != slideToInt) {
          slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
        }
      });
    }

  });

});
.slider-wrap {
  position: relative;
  width: 700px;
  margin: 0 auto
}

#secondary-slider {
  position: absolute;
  top: 0;
  right: 0;
  width: 50%;
}

.flex-direction-nav .flex-prev,
.flexslider:hover .flex-direction-nav .flex-prev,
.flexslider:hover .flex-direction-nav .flex-next,
.flex-direction-nav .flex-next {
  top: unset;
  left: unset;
  right: unset;
  bottom: unset;
}

.flexslider:hover .flex-direction-nav .flex-prev:hover,
.flexslider:hover .flex-direction-nav .flex-prev:hover {
  opacity: 1;
}

#main-slider .flex-direction-nav .flex-prev,
#secondary-slider .flex-direction-nav .flex-prev {
  position: absolute;
  top: 20px !important;
  right: 0 !important;
  opacity: 1;
}

#main-slider .flex-direction-nav .flex-next,
#secondary-slider .flex-direction-nav .flex-next {
  position: absolute;
  top: 120px !important;
  right: 0;
  opacity: 1;
}

1 个答案:

答案 0 :(得分:2)

您可以通过创建自己的箭头来应用与链接相同的逻辑。删除默认的监听器,然后添加自己的侦听器以移动滑块。

对于活动链接,只需添加更多JS即可切换类。

在此示例中,您可以轻松地使用CSS随意调整位置:

jQuery(document).ready(function($) {

  $('#main-slider').flexslider({
    animation: "slide",
    slideToStart: 0,
    start: function(slider) {
      $('a.slide_thumb').click(function() {
        $('.active').removeClass('active');
        $(this).addClass('active');
        $('.flexslider').show();
        var slideTo = $(this).attr("rel") //Grab rel value from link;
        var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
        if (slider.currentSlide != slideToInt) {
          slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
        }
      });
      $('.flex-prev').click(function(e) {
        e.preventDefault();
        $('.active').removeClass('active');
        $('.flexslider').show();
        var c = slider.currentSlide;
        if(c!=0) {
          c--;
          slider.flexAnimate(c);
          $('.slide_thumb[rel='+c+']').addClass('active');
        } else {
          slider.flexAnimate(3);
          $('.slide_thumb[rel=3]').addClass('active');
        }
      })
       $('.flex-next').click(function(e) {
       e.preventDefault();
        $('.active').removeClass('active');
       $('.flexslider').show();
        var c = slider.currentSlide;
        if(c!=3) {
          c++;
          slider.flexAnimate(c);
          $('.slide_thumb[rel='+c+']').addClass('active');
        } else {
          slider.flexAnimate(0);
          $('.slide_thumb[rel=0]').addClass('active');
        }
      })
    }

  });

  $('#secondary-slider').flexslider({
    animation: "slide",
    slideToStart: 0,
    start: function(slider) {
      $('a.slide_thumb').click(function() {
      $('.active').removeClass('active');
        $(this).addClass('active');
        $('.flexslider').show();
        var slideTo = $(this).attr("rel") //Grab rel value from link;
        var slideToInt = parseInt(slideTo) //Make sure that this value is an integer;
        if (slider.currentSlide != slideToInt) {
          slider.flexAnimate(slideToInt) //move the slider to the correct slide (Unless the slider is also already showing the slide we want);
        }
      });
      $('.flex-prev').click(function(e) {
      e.preventDefault();
        $('.active').removeClass('active');
      $('.flexslider').show();
        var c = slider.currentSlide;
        if(c!=0) {
          c--;
          slider.flexAnimate(c);
          $('.slide_thumb[rel='+c+']').addClass('active');
        } else {
          slider.flexAnimate(3);
          $('.slide_thumb[rel=3]').addClass('active');
        }
      })
       $('.flex-next').click(function(e) {
       e.preventDefault();
        $('.active').removeClass('active');
       $('.flexslider').show();
        var c = slider.currentSlide;
        if(c!=3) {
          c++;
          slider.flexAnimate(c);
          $('.slide_thumb[rel='+c+']').addClass('active');
        } else {
          slider.flexAnimate(0);
          $('.slide_thumb[rel=0]').addClass('active');
        }
      })
    }

  });

});
.slider-wrap {
  position: relative;
  width: 700px;
  margin: 0 auto
}

#secondary-slider {
  position: absolute;
  top: 0;
  right: 0;
  width: 50%;
}
.flexslider .flex-direction-nav {
 display:none;
}

.active {
  color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.1/jquery.flexslider.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.7.1/flexslider.css" rel="stylesheet" />
<div class="slider-wrap">
  <div id="main-slider" class="flexslider">
    <ul class="slides">
      <li>
        <img src="https://via.placeholder.com/350x150" />
      </li>
      <li>
        <img src="https://via.placeholder.com/350x150" />
      </li>
      <li>
        <img src="https://via.placeholder.com/350x150" />
      </li>
      <li>
        <img src="https://via.placeholder.com/350x150" />
      </li>
    </ul>
  </div>

  <div id="secondary-slider" class="flexslider">
    <ul class="slides">
      <li>
        <p>Text 1</p>
      </li>
      <li>
        <p>Text 2</p>
      </li>
      <li>
        <p>Text 3</p>
      </li>
      <li>
        <p>Text 4</p>
      </li>
    </ul>
  </div>
  <ul class="flex-direction-nav"><li class="flex-nav-prev"><a class="flex-prev" href="#">Previous</a></li><li class="flex-nav-next"><a class="flex-next" href="#">Next</a></li></ul>
  <a rel="0" class="slide_thumb" href="#">slide link 1</a>
  <a rel="1" class="slide_thumb" href="#">slide link 2</a>
  <a rel="2" class="slide_thumb" href="#">slide link 3</a>
  <a rel="3" class="slide_thumb" href="#">slide link 3</a>