Owl Carrousel交换了多个div

时间:2017-11-15 20:05:47

标签: javascript jquery onclick swap owl-carousel

我正在努力实现" div总是在点击名为&#34的按钮时交换到下一个div; 立即投票 "在猫头鹰旋转木马"但它只有一次交换精确到下一个div点击按钮,其他时间它没有准确交换到下一个div这里是我的fiddle链接。

<script type="text/javascript">     
    $(document).ready(function() {          
        $(".go-swap").click(function() {            
            $("#currnt").swap({
                target: "next",
                opacity: "0.5",
                speed: 1000,                    
            });         
        });         
    });
</script>
<script type="text/javascript">     
    $(document).ready(function() {          
        $('.item').hover(function(){ 
            $(this).attr('id', 'currnt'); 
            $(this).parent().prev().children().attr('id', 'next')},                 
            function(){
                $(this).attr('id', '');
                $(this).parent().prev().children().attr('id', '') 
            });
        }); 
</script>
<section id="demos">
  <div class="row">
    <div class="large-12 columns">
      <div class="owl-carousel owl-theme">
        <div class="item" data-hash="zero">
          <h4>0</h4>
            <a href="javascript://" class="go-swap"><button>Vote Now</button></a>
        </div>

        <div class="item" data-hash="one">
          <h4>1</h4>
            <a href="javascript://" class="go-swap"><button>Vote Now</button></a>
        </div>
        <div class="item" data-hash="two">
          <h4>2</h4>
            <a href="javascript://" class="go-swap"><button>Vote Now</button></a>
        </div>
        <div class="item" data-hash="three">
          <h4>3</h4>
            <a href="javascript://" class="go-swap"><button>Vote Now</button></a>
        </div>
        <div class="item" data-hash="four">
          <h4>4</h4>
            <a href="javascript://" class="go-swap"><button>Vote Now</button></a>
        </div>
      </div>
    </div>
  </div>
</section>

2 个答案:

答案 0 :(得分:0)

要理解你想要达到的目标有点难,你可以点击这样的下一张幻灯片

$(document).ready(function() {
      var slider =  $('.owl-carousel')

    slider.owlCarousel({
        items: 4,
        loop: false,
        center: false,
        margin: 10,
        callbacks: true,
        URLhashListener: true,
        autoplayHoverPause: true,
        startPosition: 'URLHash'
      });

     $(".go-swap").click(function() {
            slider.trigger('next.owl.carousel');
      });

});

在这里摆弄 - https://jsfiddle.net/Jim_Miraidev/uu8ukac6/

这是你在寻找什么?

答案 1 :(得分:0)

//swap to next div

        var animating = false;

        $('.owl-carousel').on('click', '.swap', function() {

        var clickedDiv = $(this).closest('.owl-item'),

        prevDiv = clickedDiv.prev(),
        distance = clickedDiv.offset().left - prevDiv.offset().left;

          if (prevDiv.length) {
            animating = true;
            $.when(clickedDiv.animate({
                left: -distance
              }, 1000),
              prevDiv.animate({
                left: distance
              }, 1000)).done(function() {
              prevDiv.css('left', '0px');
              clickedDiv.css('left', '0px');
              clickedDiv.insertBefore(prevDiv);
              animating = false;
            });
            }
            });

// owl carousel
$(document).ready(function() {
 var slider = $('.owl-carousel');
  slider.owlCarousel({
    items: 4,
    loop: false,
    center: false,
    margin: 10,
    callbacks: true,
    URLhashListener: true,
    autoplayHoverPause: true,
    startPosition: 'URLHash',
  touchDrag: false,
  mouseDrag: false
  });
  $(".swap").click(function() {
        slider.trigger('prev.owl.carousel');
  });
})

https://jsfiddle.net/yajuvendra1990/b8qx6jjt/9/