我scroll-snap-type
习惯于在 DIV 中移动并将它们放入视图中。我想要做的是默认将视图设置为第二个 DIV,因此当页面加载时会看到“默认情况下应该查看”的 div,然后用户可以向左或向右滚动。
我怎么能用 CSS 做到这一点?(没有 JS)
https://jsfiddle.net/2hcgoL1b/2/
html, body, .holster {
height: 100%;
}
.container {
display: flex;
overflow: auto;
outline: 1px dashed lightgray;
flex: none;
}
.container.x {
width: 100%;
height: 128px;
flex-flow: row nowrap;
}
/* scroll-snap */
.x.mandatory-scroll-snapping {
scroll-snap-type: x mandatory;
}
.container > div {
text-align: center;
scroll-snap-align: center;
flex: none;
}
.x.container > div {
line-height: 128px;
font-size: 64px;
width: 100%;
height: 128px;
}
/* appearance fixes */
.y.container > div:first-child {
line-height: 1.3;
font-size: 64px;
}
/* coloration */
.container > div:nth-child(even) {
background-color: #87EA87;
}
.container > div:nth-child(odd) {
background-color: #87CCEA;
}
<div class="container x mandatory-scroll-snapping" dir="ltr">
<div>1</div>
<div>This should be viewed by default</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>