CSS Grid与CSS滚动捕捉功能不能正常工作

5

当使用CSS滚动捕捉与Flexbox时,捕捉效果非常好:

* {
  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配合使用?

1
也许这是因为 flex 线是畅通无阻的,而网格线包含轨道墙的原因? https://dev59.com/36vka4cB1Zd3GeqPpjbM - Michael Benjamin
2个回答

1

我认为问题出在滚动容器的设置宽度上。 将滚动容器的width改为auto,并将grid-template-columns更改为repeat(5, 100vw)后,对我有用:

* {
  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(5, 100vw);
  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>

我不确定为什么会发生这种情况,但也许其他人有想法。


0

- 这种行为的原因是什么?

对于这个第一个问题,我没有答案。

- 有没有办法让CSS Grid和CSS Snap一起工作?

是的,Max Kohler(在CSS Scroll Snapping tutorial中)创建了一个带有捕捉功能的CSS网格,但是他需要使用名为css-scroll-snap-polyfill的JS独立脚本才能使捕捉功能正常工作。

链接到他创建的Codepen演示:https://codepen.io/maxakohler/pen/MBWJKm

要使用他的方法,您需要将此JS脚本添加到您的网站中:

<script src="https://bundle.run/css-scroll-snap-polyfill@0.1.2"></script>

然后当DOM加载完成时调用此JS函数:

cssScrollSnapPolyfill()

它应该像他的Codepen演示一样工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接