删除owlCarousel中某个宽度的stagePadding

时间:2018-01-02 10:00:39

标签: javascript jquery css owl-carousel

有人知道我可以访问owlCarousel中的stagePadding属性,所以如果窗口宽度小于x px,我可以将其设置为0。所以,像这样:

function() {
    $('.owl-carousel').owlCarousel({
        margin: 10,
        loop: true,
        items: 1,
        stagePadding: 200,
        nav: true,
        dots: true,
        dotsData: true,
        navText : ["<span>←</span>","<span>→</span>"],
        dotsContainer: '.owl-dots-container'
    });

    if ( $(window).width() < 1000 ) {
        $('.owl-carousel').stagePadding = 0;
    } else {
        $('.owl-carousel').stagePadding = 200;
    }
});

4 个答案:

答案 0 :(得分:3)

OwlCarousel有一个方便的选项已经内置。非常简单 - 只需添加“响应”选项并输入必要断点的宽度(0,1000等...):

    $('.owl-carousel').owlCarousel({
        margin: 10,
        loop: true,
        items: 1,
        nav: true,
        dots: true,
        dotsData: true,
        navText : ["<span>←</span>","<span>→</span>"],
        dotsContainer: '.owl-dots-container',
        responsive: {
            0: {
                stagePadding: 0
            },
            1000: {
                stagePadding: 200
            }
        }
    });

答案 1 :(得分:1)

你可以改为使用这样的代码:

function() {
  if ($(window).width() < 1000) {
    $('.owl-carousel').owlCarousel({
      margin: 10,
      loop: true,
      items: 1,
      stagePadding: 0,
      nav: true,
      dots: true,
      dotsData: true,
      navText: ["<span>←</span>", "<span>→</span>"],
      dotsContainer: '.owl-dots-container'
    });
  } else {
    $('.owl-carousel').owlCarousel({
      margin: 10,
      loop: true,
      items: 1,
      stagePadding: 200,
      nav: true,
      dots: true,
      dotsData: true,
      navText: ["<span>←</span>", "<span>→</span>"],
      dotsContainer: '.owl-dots-container'
    });
  }
});

答案 2 :(得分:1)

也可以这样做吗?

$(function(){
    let $owlCarouselContainer = $('.owl-carousel');
    let windowWidth = $(window).width();
    let owlCarouselProps = {
      margin: 10,
      loop: true,
      items: 1,
      stagePadding: windowWidth < 1000 ? 0 : 200,
      nav: true,
      dots: true,
      dotsData: true,
      navText: ["<span>←</span>", "<span>→</span>"],
      dotsContainer: '.owl-dots-container'
    };
    $owlCarouselContainer.owlCarousel(owlCarouselProps);
});

答案 3 :(得分:1)

您可以声明变量,然后根据您的条件设置其值。

var stgPadding;
 if ( $(window).width() < 1000 ) {
                stgPadding = 0;
            } else {
                stgPadding = 200;
            }
function() {
            $('.owl-carousel').owlCarousel({
                margin: 10,
                loop: true,
                items: 1,
                stagePadding: stgPadding,
                nav: true,
                dots: true,
                dotsData: true,
                navText : ["<span>←</span>","<span>→</span>"],
                dotsContainer: '.owl-dots-container'
            });
        });