如何更改为不同屏幕尺寸显示的项目数

时间:2016-03-23 05:35:00

标签: javascript jquery carousel cycle2

我试图找到一种方法来更改不同屏幕尺寸的carouselVisible项目数量。我希望在768px的屏幕分辨率下显示3个项目,当你在360中缩小以显示1个项目时。

这可能吗?

Demo

jquery的

$('#carousel').cycle({
allowWrap: true,
carouselVisible: 5,
prev: '#prev',
next: '#next',
carouselFluid: true,
timeout: 0,
slides: 'article',
fx: 'carousel'
});

var slideshows1 = $('#carousel').on('cycle-next cycle-prev', function (e, opts) {
slideshows1.not(this).cycle('goto', opts.currSlide);
});
var slideshows2 = $('#carousel1').on('cycle-next cycle-prev', function (e, opts) {
slideshows2.not(this).cycle('goto', opts.currSlide);
});
$('#carousel article').click(function () {
var count = $("#carousel1 .readmore").length - 1;
var selectedIndex = $('#carousel').data('cycle.API').getSlideIndex(this);
var index = selectedIndex<count ? selectedIndex: (selectedIndex-count)%count;
slideshows1.cycle('goto', index);
slideshows2.cycle('goto', index);
});

Html

        <div class="service">
         <h1>Lead1</h1>
    </div>
</article>
<article>
    <div class="service">
         <h1>Lead2</h1>
    </div>
</article>
</div>
<div id="carousel1" data-allow-wrap="true" data-cycle-prev="#prev" data-cycle-next="#next" class="cycle-slideshow" data-cycle-timeout="0" data-cycle-manual-fx="scrollHorz" data-cycle-slides=".readmore">
<div class="readmore">
     <h2 class="lead">Lead</h2>

    <p>Testing some text right here</p> <a class="cta" href="#">Läs mer</a>

</div>
<div class="readmore">
     <h2 class="lead">Lead1</h2>

    <p>Testing some text right here</p> <a class="cta" href="#">Läs mer</a>

</div>
<div class="readmore">
     <h2 class="lead">Lead2</h2>

    <p>Testing some text right here</p> <a class="cta" href="#">Läs mer</a>

</div>
</div>
<div id="next">next</div>
<div id="prev">prev</div>

1 个答案:

答案 0 :(得分:1)

取决于,如果您需要动态调整,当您的用户调整页面大小时,您会执行以下操作:

var properties = {
    allowWrap: true,
    carouselVisible: 5,
    prev: '#prev',
    next: '#next',
    carouselFluid: true,
    timeout: 0,
    slides: 'article',
    fx: 'carousel'
}

$(window).resize(function() {
    var width = $(window).width();
    var height = $(window).height();
    var slideAmount;

    if (width >= 768) {
        slideAmount = 3;
    } else if (width <= 360) {
        slideAmount = 1;
    } else {
        slideAmount = 2;
    }

    if (properties.carouselVisible != slideAmount) {
        $('#carousel').cycle('reinit');
    }
});

如果您只想做一次,那么,显然,您只需获得初始屏幕尺寸:

var width = $(window).width();
var height = $(window).height();

// Instantiate your carousel with parameters based on screen size