在Flexbox中使用CSS滚动捕捉时,捕捉效果很好:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.slider {
font-family: sans-serif;
scroll-snap-type: mandatory;
scroll-snap-points-y: repeat(100vw);
scroll-snap-type: x mandatory;
display: flex;
overflow-x: scroll;
}
section {
border-right: 1px solid white;
padding: 1rem;
min-width: 100vw;
height: 100vh;
scroll-snap-align: start;
text-align: center;
position: relative;
}
h1 {
position: absolute;
top: 50%;
transform: translateY(-50%);
text-align: center;
color: red;
width: 100%;
left: 0;
font-size: calc(1rem + 3vw);
}
<div class="slider">
<section>
<h1>Section One</h1>
</section>
<section>
<h1>Section Two</h1>
</section>
<section>
<h1>Section Three</h1>
</section>
<section>
<h1>Section Four</h1>
</section>
<section>
<h1>Section Five</h1>
</section>
</div>
但是,当尝试将CSS Grid与CSS Snap一起使用时,它似乎不起作用:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.slider {
font-family: sans-serif;
scroll-snap-type: mandatory;
scroll-snap-points-y: repeat(100vw);
scroll-snap-type: x mandatory;
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(auto-fill, 100vw);
overflow-x: scroll;
width: 600vw;
}
section {
border-right: 1px solid white;
padding: 1rem;
min-width: 100vw;
height: 100vh;
scroll-snap-align: start;
text-align: center;
position: relative;
}
h1 {
position: absolute;
top: 50%;
transform: translateY(-50%);
text-align: center;
color: red;
width: 100%;
left: 0;
font-size: calc(1rem + 3vw);
}
<div class="slider">
<section>
<h1>Section One</h1>
</section>
<section>
<h1>Section Two</h1>
</section>
<section>
<h1>Section Three</h1>
</section>
<section>
<h1>Section Four</h1>
</section>
<section>
<h1>Section Five</h1>
</section>
</div>
此行为的原因是什么,有没有办法使CSS Grid与CSS Snap一起使用?