【问题标题】:How to change the number of items displayed for different screen sizes如何更改不同屏幕尺寸显示的项目数
【发布时间】:2016-07-10 07:03:02
【问题描述】:

我正在尝试找到一种方法来更改不同屏幕尺寸的 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>

【问题讨论】:

  • 你可以通过为不同的窗口大小编写不同的代码来做到这一点:) if ( $(window).width() &gt; 739) { //Add your javascript for large screens here } else { //Add your javascript for small screens here }
  • 非常感谢,能给我演示一下吗
  • 根据需要改变宽度
  • 当宽度>739时检查以下截图(你给自己的宽度)picpaste.com/…当宽度picpaste.com/…

标签: javascript jquery carousel cycle2


【解决方案1】:

您可以轻松使用 swiperjs,并且可以设置断点。举例

    <Swiper
    slidesPerView={1}
    spaceBetween={10}
    pagination={{
      clickable: true
    }}
    breakpoints={{
      "640": {
        slidesPerView: 2,
        spaceBetween: 20
      },
      "768": {
        slidesPerView: 4,
        spaceBetween: 40
      },
      "1024": {
        slidesPerView: 5,
        spaceBetween: 50
      }
    }}
    className="mySwiper"
  >
    <SwiperSlide>Slide 1</SwiperSlide>
    <SwiperSlide>Slide 2</SwiperSlide>
    <SwiperSlide>Slide 3</SwiperSlide>
    <SwiperSlide>Slide 4</SwiperSlide>
    <SwiperSlide>Slide 5</SwiperSlide>
    <SwiperSlide>Slide 6</SwiperSlide>
    <SwiperSlide>Slide 7</SwiperSlide>
    <SwiperSlide>Slide 8</SwiperSlide>
    <SwiperSlide>Slide 9</SwiperSlide>
  </Swiper>

【讨论】:

    【解决方案2】:

    取决于,如果您需要动态调整,当您的用户调整页面大小时,您会这样做:

    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
    

    【讨论】:

    • 只需将轮播选项存储在单独的对象中,然后检查 if (width
    猜你喜欢
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 2017-03-28
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多