代码之家  ›  专栏  ›  技术社区  ›  Mohammed Wahed Khan Michael

同步柔性滑块-柔性方向导航(箭头)

  •  0
  • Mohammed Wahed Khan Michael  · 技术社区  · 6 年前

    我当时正在使用flex-Slider制作滑块。我有一个同步到一个我们做猫头鹰旋转木马两个flexsliders的想法。当flex-caption使用图像制作动画时,我必须使用两个flexslider,因为我希望flex-caption动画与图像动画不同。我已经把它们和这个同步了 Answer 但现在我被困在两点:

    1) 我也要同步箭头。正如您在我的代码中看到的,我已经将flex-direction导航分组。现在我想在点击一个按钮时更改两张幻灯片,但这不起作用,因为上面有分层。

    2) 我希望自定义目标按钮是活动的,取决于活动幻灯片。例如,我在第三张幻灯片上,我希望第三个目标锚定标记处于活动状态,比如颜色的改变等等。

    希望我能把我的问题解释清楚。如果您需要任何进一步的信息,请在下面评论。

    这是你的名字 Codepen 链接。后面是代码。

    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;
    }
    <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>
      <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>
    1 回复  |  直到 6 年前
        1
  •  2
  •   Temani Afif    6 年前

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

    对于活动链接,只需添加更多的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>