简体   繁体   English

使用滚动捕捉和弯曲时将 CSS slider 居中

[英]Center a CSS slider when using scroll-snap and flex

I am trying to create a basic slider with 3 slides.我正在尝试用 3 张幻灯片创建一个基本的 slider。 The idea is to show the second slide on initial view, if the user swipes left, the slider will show the first slide, if the user swipes right it will show the right slide.这个想法是在初始视图上显示第二张幻灯片,如果用户向左滑动,slider 将显示第一张幻灯片,如果用户向右滑动它将显示右侧幻灯片。

I have the initial slider working but I can't centre it.我有最初的 slider 工作,但我无法居中。 If I use justify-content on the main wrapper, I'm unable to slide to the first slide.如果我在主包装器上使用 justify-content,我将无法滑到第一张幻灯片。

Here's the code这是代码

 body { -webkit-overflow-scrolling: touch; }.wrapper { scroll-snap-type: x mandatory; scroll-padding: 0px; scroll-padding-left: 0px; display: flex; overflow-x: scroll; height: 400px; width: 100vw; /* justify-content: center; */ }::-webkit-scrollbar { display: none; }.item { scroll-snap-align: start; scroll-snap-stop: always; flex: 0 0 calc(100vw); height: 400px; text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center; text-transform: uppercase; color: whitesmoke; font-size: 64px; }.item-a { background-color:green; flex: 0 0 calc(150px); }.item-b { background-color:red; }.item-c { background-color:blue; flex: 0 0 calc(150px); }
 <main> <div class="wrapper"> <div id="item-a" class="item item-a">a</div> <div id="item-b" class="item item-b">b</div> <div id="item-c" class="item item-c">c</div> </div> </main>

justify-content: center;

with

justify-content: space-between;

I think shoud work for your example.我认为应该为你的例子工作。 here is an explaination from developer mozilla site这是开发者 Mozilla 网站的解释

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

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