猫头鹰走马灯,到达第一个/最后一个项目后导航被禁用

14

我想在我的网站中使用Owl Carousel插件。我希望在抵达第一个/最后一个项目后禁用导航,例如通过在导航中添加“disabled”类,然后通过CSS禁用它。这是否可能?

我的代码:

$(document).ready(function() {
  var owl = $("#owl-demo");
  owl.owlCarousel({
    rewindNav : false, 
    pagination : false,        
    items : 4
  });
  // Custom Navigation Events
  $(".next").click(function(){
    owl.trigger('owl.next');
  })
  $(".prev").click(function(){
    owl.trigger('owl.prev');
  })
});
.item { background: #e5e5e5; margin: 10px}
.btn { background: #bd0000; color: #fff; padding: 5px 10px; cursor: pointer}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://owlgraphic.com/owlcarousel/owl-carousel/owl.carousel.js"></script>
<link href="http://owlgraphic.com/owlcarousel/owl-carousel/owl.carousel.css" rel="stylesheet" />

<div id="owl-demo" class="owl-carousel owl-theme">
  <div class="item"><h1>1</h1></div>
  <div class="item"><h1>2</h1></div>
  <div class="item"><h1>3</h1></div>
  <div class="item"><h1>4</h1></div>
  <div class="item"><h1>5</h1></div>
  <div class="item"><h1>6</h1></div>
  <div class="item"><h1>7</h1></div>
  <div class="item"><h1>8</h1></div>
  <div class="item"><h1>9</h1></div>
  <div class="item"><h1>10</h1></div>
</div>

<div class="customNavigation">
  <a class="btn prev">Previous</a>
  <a class="btn next">Next</a>
</div>

http://jsfiddle.net/p3d52z4n/1/


如果您不使用自定义按钮,则可以按照以下方式完成:http://jsfiddle.net/hr5ucj77/ - Christina
9个回答

16

您可以在使用自定义控件后,使用回调函数afterAction。

afterAction: function(){
  if ( this.itemsAmount > this.visibleItems.length ) {
    $('.next').show();
    $('.prev').show();

    $('.next').removeClass('disabled');
    $('.prev').removeClass('disabled');
    if ( this.currentItem == 0 ) {
      $('.prev').addClass('disabled');
    }
    if ( this.currentItem == this.maximumItem ) {
      $('.next').addClass('disabled');
    }

  } else {
    $('.next').hide();
    $('.prev').hide();
  }
}

查看工作演示- http://jsfiddle.net/p3d52z4n/9/


10

最简单的解决方案:

OwlCarousel 2 在到达第一个/最后一个元素时,会为导航元素添加disabled类。

因此,你只需要像这样做:

.owl-nav{
  .disabled{
    display: none;
  }
}

6

只需要使用回调函数-

loop:false,
navRewind: false

你会注意到当到达第一个/最后一个项目时,'disabled'类将被添加到owl-next和owl-prev。 添加CSS-
.owl-next.disabled, .owl-prev.disabled {
display: none !important;
}

会做到位。

3

我也遇到了Owl Carousel 2的同样问题,我的解决方案是,在调用幻灯片后使用本地导航按钮:

             var elm = '.slider'; //your slider class
             function toggleArrows(){ 
                if($(elm).find(".owl-item").last().hasClass('active') && 
                   $(elm).find(".owl-item.active").index() == $(elm).find(".owl-item").first().index()){                       
                    $(elm).find('.owl-nav .owl-next').addClass("off");
                    $(elm).find('.owl-nav .owl-prev').addClass("off"); 
                }
                //disable next
                else if($(elm).find(".owl-item").last().hasClass('active')){
                    $(elm).find('.owl-nav .owl-next').addClass("off");
                    $(elm).find('.owl-nav .owl-prev').removeClass("off"); 
                }
                //disable previus
                else if($(elm).find(".owl-item.active").index() == $(elm).find(".owl-item").first().index()) {
                    $(elm).find('.owl-nav .owl-next').removeClass("off"); 
                    $(elm).find('.owl-nav .owl-prev').addClass("off");
                }
                else{
                    $(elm).find('.owl-nav .owl-next,.owl-nav .owl-prev').removeClass("off");  
                }
            }

            //turn off buttons if last or first - after change
            $(elm).on('initialized.owl.carousel', function (event) {
                toggleArrows();
            });
             $(elm).on('translated.owl.carousel', function (event) { toggleArrows(); });

关于CSS,给"off"类赋予禁用按钮的属性。

2

我使用Owl Carousel 2正常运行

$('#owlCarousel').owlCarousel({
            loop:true,
            loop:false,
            responsiveClass:true,            
            responsive:{
                0:{
                    items:1,
                    nav:true
                },
                600:{
                    items:3,
                    nav:true
                },
                1000:{
                    items:4,
                    nav:true,                    
                    touchDrag:false,
                    //pullDrag:false,
                    freeDrag:false
                }                
            },
            onTranslated:callBack
        });
        function callBack(){
          if($('.owl-carousel .owl-item').last().hasClass('active')){
                $('.owl-next').hide();
                $('.owl-prev').show(); 
                console.log('true');
             }else if($('.owl-carousel .owl-item').first().hasClass('active')){
                $('.owl-prev').hide(); 
                $('.owl-next').show();
                console.log('false');
             }
        }
        $('#owlCarousel .owl-prev').hide();

循环:真, 循环:假 - Spikatrix
我们可以只使用一个循环:true或false :) - Brahmanand Gorati
我知道,但是为什么你的代码中要同时拥有它们呢? - Spikatrix

1
正如之前所说,您可以使用Owl的回调函数来隐藏或更改“下一页”按钮。但是,您实际上可以非常简单地禁用它,而不是使用一些 disabled 类来告诉用户该按钮不应再使用:
$slider.on('changed.owl.carousel', ev => {
    const carousel = ev.currentTarget
    $('.owl-next', $el).attr('disabled', carousel.current() === carousel.maximum())
    $('.owl-prev', $el).attr('disabled', carousel.current() === carousel.minimum())
})

你可以使用CSS选择器[disabled]来为禁用的按钮设置样式。

1
我正在寻找解决方案,我找到了一些代码并将它们结合起来。对我有效。当第一个项目向左箭头隐藏时,最后一个项目向右箭头隐藏。
请注意.on()事件。
$('.homeSlider').owlCarousel({
    loop: false ,
    autoplay: false,
    navClass: ['fa fa-chevron-left', 'fa fa-chevron-right'],
    navText: ['', ''],
    margin: 20,
    startPosition: -0,
    items: 3,
    nav: true,
    dots: false,
    center: false,
    autoWidth: true,
    responsive: {
        0: {
            items: 1
        },
        600: {
            items:2,
            margin: 20,
            startPosition: 0,
            loop: true,
            autoWidth: true,
            center: false

        },
        992: {
            items: 3
        },
        1920: {
            items: 5
        }
    }}).on('initialized.owl.carousel changed.owl.carousel refreshed.owl.carousel', function (event) {
    //alert("s");
    if (!event.namespace) return;
    var carousel = event.relatedTarget,
        element = event.target,
        current = carousel.current();

    if(current === carousel.maximum()) $('.homeSlider .fa-chevron-right').hide();
    if(current === carousel.minimum()) $('.homeSlider .fa-chevron-left').hide();

});
$('.homeSlider .fa-chevron-left').hide();

1
在Owl Carousel 2中使用自定义导航,适用于我。
            onTranslated: function(event){
                if (event.item.index == 0) jQuery("#owlPrev").hide();
                else jQuery("#owlPrev").show();

                if (event.item.index == (event.item.count - 1)) jQuery("#owlNext").hide();
                else jQuery("#owlNext").show();
            }

也注意到响应式方法可以有多个回调函数,例如:
    responsive:{
        0:{
            items: 1,
            slideBy: 1,
            onTranslated: function(event){
                if (event.item.index == 0) jQuery("#owlPrev").hide();
                else jQuery("#owlPrev").show();

                if (event.item.index == (event.item.count - 1)) jQuery("#owlNext").hide();
                else jQuery("#owlNext").show();
            }
        },
        992:{
            items: 2,
            slideBy: 2,
            onTranslated: function(event){
                if (event.item.index === 0) jQuery("#owlPrev").hide();
                else jQuery("#owlPrev").show();

                if (event.item.index == (event.item.count / 2)) jQuery("#owlNext").hide();
                else jQuery("#owlNext").show();
            }
        }
    }

0
 $('.owl-carousel').each(function (e) {
        var owl = $(this);
        if (!owl.data('owl.carousel').options.loop) {
            var options = owl.data('owl.carousel').options;
            owl.trigger('destroy.owl.carousel');
            owl.owlCarousel(options).on('changed.owl.carousel', ev => {
                if (!event.namespace) return;
                var carousel = event.relatedTarget,
                    element = event.target,
                    current = carousel.current();
                setTimeout(function () {
                    $('.owl-next').toggleClass('disabled', current === carousel.maximum());
                    $('.owl-prev').toggleClass('disabled', current === carousel.minimum());
                }, 1);
            });
        }
    });

CSS

.owl-next.disabled, .owl-prev.disabled {
    display: none !important;
}

或者你想要什么


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接