CSS水平滚动捕捉到幻灯片的顶部



(编辑:原来帖子中的JS是错误包含的,现在已经删除(

我有一个水平卷轴工作得非常好。

问题是我的幻灯片是垂直滚动的(这很好,因为有很多内容(,这意味着当我向下滚动到幻灯片1的底部,然后水平滚动到幻灯片2时,视口确实会捕捉到幻灯片2的左侧,但视口会停留在幻灯片的底部。我希望视口也捕捉到幻灯片2的顶部。

我还没有找到解决方案,似乎只有startendcenter作为scroll-snap-align的工作值,这并不能解决我的问题。

有人知道这方面的办法吗?

请参阅下面的代码片段。

#slidesContainer {
height: auto;
width: auto;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
position: relative;
overflow: scroll;
scroll-snap-type: x mandatory;
-webkit-overflow-scrolling: touch;
}
#slides {
color: black;
width: 100%;
height: auto;
font-size: 100px;
position: static;
flex-grow: 0;
flex-shrink: 0;
scroll-snap-align: start;
scroll-snap-stop: always;
}
<div id="slidesContainer">
<div id="slides" style="background-color:blue;">Slide 1<br><br> Scroll down until there is no more text, then scroll right text text text text text text text text text text text text text </div>
<div id="slides" style="background-color:green;">Slide 2<br><br>This text is not visible unless you scroll back up - this is the problem.</div>
<div id="slides" style="background-color:yellow;">Slide 3<br><br></div>
</div>

提前非常感谢。

如果我对你的理解正确,这应该很好。本质上,我通过CSS在注释/* manual scrollbar */下添加了一个自定义滚动条。你可以随心所欲地设计它,但我只是把它做得很基础。然后,您可以使用transform: rotateX(180deg);将水平滚动条放在元素的顶部。我在CSS中添加的注释应该会有所帮助。

#slidesContainer {
height: 100vh;
width: auto;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
position: relative;
overflow: scroll;
/* important for manual scrollbar to show */
}
#slides {
color: black;
width: 100%;
height: 100%;
font-size: 100px;
position: static;
flex-grow: 0;
flex-shrink: 0;
}

/* manual scrollbar */
#slidesContainer::-webkit-scrollbar {
-webkit-appearance: none;
width: 0px;
}
#slidesContainer::-webkit-scrollbar-thumb {
border-radius: 4px;
background-color: rgba(0, 0, 0, .5);
box-shadow: 0 0 1px rgba(255, 255, 255, .5);
}

/* Puts scrollbar at top */
div#slidesContainer {
transform: rotateX(180deg);
}
div#slidesContainer * {
transform: rotateX(180deg);
}

/* for good measures */
body {
margin: 0;
}
<div id="slidesContainer">
<div id="slides" style="background-color:blue;">Slide 1<br><br> text text text text text text text text text text text text text </div>
<div id="slides" style="background-color:green;">Slide 2<br><br></div>
<div id="slides" style="background-color:yellow;">Slide 3<br><br></div>
</div>

最新更新