简体   繁体   English

如何将物品放在Slick Carousel中?

[英]How can I center items in Slick Carousel?

I'm using the Slick Carousel , but there's one layout itch I can't scratch. 我正在使用Slick Carousel ,但有一个布局痒,我无法划伤。 When I have fewer items than the carousel can display at once, they're left-justified. 当我的物品数量少于旋转木马可以同时显示的物品时,它们是左对齐的。 I'd like them to be centered. 我希望他们能够集中精力。 For example, with slidesToShow equal 3 and only 2 items, I get 例如,如果slidesToShow等于3,只有2个项目,我得到

-------------------
 IIIII IIIII
-------------------

when what I want is 当我想要的是

-------------------
    IIIII IIIII
-------------------

or if there's only 1 item: 或者如果只有1个项目:

-------------------
       IIIII
-------------------

I've tried setting centerMode , but that's a completely different effect. 我试过设置centerMode ,但这是一个完全不同的效果。

Is there a way to get the effect I want? 有没有办法得到我想要的效果? It needs to handle slidesToShow being reduced by responsive breakpoints. 它需要处理slidesToShow被响应断点减少。

A quick inspection of the carousel's DOM shows that the items are put in an element with the class slick-track . 快速检查轮播的DOM显示,这些项目放在具有类型slick-track的元素中。 It is possible to center the items by modifying the margins of that element: 可以通过修改该元素的边距来居中项目:

.slick-track {
    margin:auto;
}

Demo in this JSFiddle . 演示这个JSFiddle

Note that the carousel in the demo sometimes stops working correctly when being resized while the last item is active but that happens regardless of this CSS modification. 请注意,在最后一项处于活动状态时,演示中的轮播有时会在调整大小时停止正常工作,但无论此CSS修改如何都会发生这种情况。

Putting margin:0 auto; margin:0 auto; on the <img> tag worked for me. <img>标签上为我工作。

Eg, 例如,

<div class="items">
    <div>
        <img src="https://via.placeholder.com/25x50" />
    </div>
    <div>
        <img src="https://via.placeholder.com/25x50" />
    </div>
    <div>
        <img src="https://via.placeholder.com/25x50" />
    </div>
<div>

CSS CSS

.items { background:red; }
.items img { margin:0 auto; }

https://jsfiddle.net/cegor9j2/ https://jsfiddle.net/cegor9j2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM