jquery 多个同步的猫头鹰旋转木马

slwdgvem  于 2023-10-17  发布在  jQuery
关注(0)|答案(2)|浏览(98)

我已经编辑了here提供的代码,以满足两个carousel的需要,而不依赖于id,这很好,除了我不能让标签在点击时正常工作。(例如,删除类“synced”并将它们正确地添加到单击的选项卡上。)认为我没有同时使用function syncPositionfunction left做正确的事情。
我错过了什么,或者做错了什么?
超文本标记语言:

<!--Carousel 1-->
<div class="tabslider">
  <div class="owl-carousel tabthumb">
    <div class="item" >1</div>
    <div class="item" >2</div>
    <div class="item" >3</div>
    <div class="item" >4</div>
    <div class="item" >5</div>
  </div>
  <div class="owl-carousel tabcontent">
    <div class="item">Content 1</div>
    <div class="item">Content 2</div>
    <div class="item">Content 3</div>
    <div class="item">Content 4</div>
    <div class="item">Content 5</div>
  </div>
</div>
<!--Carousel 2-->
<div class="tabslider">
<div class="owl-carousel tabthumb">
  <div class="item" >1</div>
  <div class="item" >2</div>
  <div class="item" >3</div>
  <div class="item" >4</div>
</div>
<div class="owl-carousel tabcontent">
  <div class="item">Content 1</div>
  <div class="item">Content 2</div>
  <div class="item">Content 3</div>
  <div class="item">Content 4</div>
</div>

脚本:

<script>
    $(document).ready(function() {

      $('.tabslider').each(function(){

        var sync1 = $(this).children(".tabcontent");
        var sync2 = $(this).children(".tabthumb");

        sync1.owlCarousel({
          singleItem : true,
          slideSpeed : 1000,
          pagination:false,
          afterAction : syncPosition,
          responsiveRefreshRate : 200,
        });

        sync2.owlCarousel({
          items : 3,
          itemsDesktop      : [1199,3],
          itemsDesktopSmall     : [979,3],
          itemsTablet       : [768,2],
          itemsMobile       : [479,2],
          pagination:false,
          navigation: false,
          navigationText: [
            "<i class='icon-arrow-left7'></i>",
            "<i class='icon-uniE6E2'></i>"
          ],
          responsiveRefreshRate : 100,
          afterInit : function(el){
            el.find(".owl-item").eq(0).addClass("synced");
          }
        });

        function syncPosition(el){
          var current = this.currentItem;
          // $(".tabthumb")
          $(this).find(".tabthumb")
            .find(".owl-item")
            .removeClass("synced")
            .eq(current)
            .addClass("synced")

          if($(this).children(".tabthumb").data("owlCarousel") !== undefined){
            left(current)
            console.log(this)
          }

        }

        $(this).children(".tabthumb").on("click", ".owl-item", function(e){
          e.preventDefault();
          var number = $(this).data("owlItem");
          sync1.trigger("owl.goTo",number);

        });

        function left(number){
          var sync2visible = sync2.data("owlCarousel").owl.visibleItems;

          var num = number;
          var found = false;
          for(var i in sync2visible){
            if(num === sync2visible[i]){
              var found = true;
            }
          }

          if(found===false){
            if(num>sync2visible[sync2visible.length-1]){
              sync2.trigger("owl.goTo", num - sync2visible.length+2)
            }else{
              if(num - 1 === -1){
                num = 0;
              }
              sync2.trigger("owl.goTo", num);
            }
          } else if(num === sync2visible[sync2visible.length-1]){
            sync2.trigger("owl.goTo", sync2visible[1])
          } else if(num === sync2visible[0]){
            sync2.trigger("owl.goTo", num-1)
          }
        }

      })

    });
    </script>
tag5nh1u

tag5nh1u1#

JS:

//remove the following(and its closing tag of course) :   

   $('.tabslider').each(function(){

//modify this part: 
$(this).children(".tabthumb").on("click", ".owl-item", function(e)
{
    //we modify our trigger to only apply to our current gallery, not all of them
    e.preventDefault();
    var number = $(this).data("owlItem");

    //The navigation with the thumbnails
    var $thumbnailNavWrapper = $(this).closest('your-thumbnail-navigation-class');

    //the slider with the big images .prev or .next depending on
 whether we find our slider before or next to the thumbnail navigation
    $sync1 = $thumbnailNavWrapper.prev();

    sync1.trigger("owl.goTo",number);

});

如果以上是不够的,你需要一个缩略图导航与“中心化”的风格。

function center(number){
    var sync2visible = $sync2.data("owlCarousel").owl.visibleItems;
    var num = number;

    if(num === sync2visible[0])
    {
      $sync2.trigger("owl.goTo", num-1);
    }
    else if(sync2visible.length <= 2)
    {
      $sync2.trigger("owl.goTo", num+1);
    }

    else
    {
      $sync2.trigger("owl.goTo", num-1);
    }
  }

我不确定这是否会100%帮助。我刚做了同样的问题。

wlzqhblo

wlzqhblo2#

Simply use Each method for the owl carousel's sync slider.
You can target CSS things by common classes and use ID with each method in JS selectors.

供您参考:检查这个URL。
[Codepen链接] https://codepen.io/ajay-rathore/pen/gOZXNda

<iframe height="300" style="width: 100%;" scrolling="no" title="Owl Carousel 2 uses of multiple sync sliders demo with loop/autoplay" src="https://codepen.io/ajay-rathore/embed/gOZXNda?default-tab=html%2Cresult" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true">
  See the Pen <a href="https://codepen.io/ajay-rathore/pen/gOZXNda">
  Owl Carousel 2 uses of multiple sync sliders demo with loop/autoplay</a> by Ajay Rathore (<a href="https://codepen.io/ajay-rathore">@ajay-rathore</a>)
  on <a href="https://codepen.io">CodePen</a>.
</iframe>

相关问题