【问题标题】:Cannot change horizontal scroll snap into vertical scroll snap无法将水平滚动捕捉更改为垂直滚动捕捉
【发布时间】:2021-07-14 17:30:41
【问题描述】:

我找到了这个codepen,但它是水平的。当我试图让它垂直时,它不起作用。

Pen我找到了代码:

// None!
/* Hide the scroll bars */

body {
  overflow-y: hidden;
}

.slides {
  /* We set the scroll snapping */
  scroll-snap-type: x mandatory;
  /* Necessary for mobile scrolling */
  -webkit-overflow-scrolling: touch;
  /* For layout purposes */
  display: flex;
  /* To allow horizontal scrolling */
  overflow-x: scroll;
}

section {
  /* For styling purposes */
  height: 100vh;
  min-width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background-repeat: no-repeat;
  background-size: cover;
  /* Set where the snapping should happen */
  scroll-snap-align: start;
}


/* Styling for the sections */

section h2 {
  font-family: system-ui, sans-serif;
  font-size: 2rem;
  color: #1a1a1a;
  font-weight: 100;
}

section h3 {
  font-family: system-ui, sans-serif;
  font-size: 2rem;
  color: #ffffff;
  font-weight: 100;
}

.section-1 {
  background-image: url('https://images.unsplash.com/photo-1524260855046-f743b3cdad07?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1401&q=80')
}

.section-2 {
  background-image: url('https://images.unsplash.com/34/BA1yLjNnQCI1yisIZGEi_2013-07-16_1922_IMG_9873.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80')
}

.section-3 {
  background-image: url('https://images.unsplash.com/photo-1500964757637-c85e8a162699?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1378&q=80')
}

.section-4 {
  background-image: url('https://images.unsplash.com/photo-1501791330673-603715379ded?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')
}
<main class="slides">
  <section class="section-1">
    <h2>Scroll right to see it in action ????</h2>
  </section>
  <section class="section-2">
    <h3>Woah! That's a cool effect!</h3>
  </section>
  <section class="section-3">
    <h2>And no Javascript! ????</h2>
  </section>
  <section class="section-4">
    <h3>How cool is that?</h3>
  </section>
</main>

我试图将水平滚动捕捉更改为垂直滚动捕捉:

// None!
/* Hide the scroll bars */

body {
  overflow-x: hidden;
}

.slides {
  /* We set the scroll snapping */
  scroll-snap-type: y mandatory;
  /* Necessary for mobile scrolling */
  -webkit-overflow-scrolling: touch;
}

section {
  /* For styling purposes */
  height: 100vh;
  min-width: 100vw;
  align-items: center;
  justify-content: center;
  background-repeat: no-repeat;
  background-size: cover;
  /* Set where the snapping should happen */
  scroll-snap-align: start;
}


/* Styling for the sections */

section h2 {
  font-family: system-ui, sans-serif;
  font-size: 2rem;
  color: #1a1a1a;
  font-weight: 100;
}

section h3 {
  font-family: system-ui, sans-serif;
  font-size: 2rem;
  color: #ffffff;
  font-weight: 100;
}

.section-1 {
  background-image: url('https://images.unsplash.com/photo-1524260855046-f743b3cdad07?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1401&q=80')
}

.section-2 {
  background-image: url('https://images.unsplash.com/34/BA1yLjNnQCI1yisIZGEi_2013-07-16_1922_IMG_9873.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80')
}

.section-3 {
  background-image: url('https://images.unsplash.com/photo-1500964757637-c85e8a162699?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1378&q=80')
}

.section-4 {
  background-image: url('https://images.unsplash.com/photo-1501791330673-603715379ded?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')
}
<main class="slides">
  <section class="section-1">
    <h2>Scroll down to see it in action ????</h2>
  </section>
  <section class="section-2">
    <h3>Woah! That's a cool effect!</h3>
  </section>
  <section class="section-3">
    <h2>And no Javascript! ????</h2>
  </section>
  <section class="section-4">
    <h3>How cool is that?</h3>
  </section>
</main>
我尝试将所有 x 更改为 y,将所有 y 更改为 x,以及不同的组合,但我仍然找不到解决方案。

【问题讨论】:

    标签: html css scroll-snap


    【解决方案1】:

    您的父元素没有固定的高度,并且会扩展以适应子元素。最重要的是,您还需要为父元素设置overflow-y: scroll;

    // None!
    /* Hide the scroll bars */
    
    body {
      margin: 0;
      padding: 0;
      overflow-x: hidden;
    }
    
    .slides {
      /* We set the scroll snapping */
      scroll-snap-type: y mandatory;
      /* Necessary for mobile scrolling */
      -webkit-overflow-scrolling: touch;
      display: flex;
      flex-flow: column;
      /* hide horizontal overflow */
      overflow-x: hidden;
      /* allow vertical overflow */
      overflow-y: scroll;
      /* set the height for parent, overwise it will expand to fit children */
      height: 100vh;
    }
    
    section {
      /* For styling purposes */
      display: flex;
      min-height: 100vh;
      width: 100vw;
      align-items: center;
      justify-content: center;
      background-repeat: no-repeat;
      background-size: cover;
      /* Set where the snapping should happen */
      scroll-snap-align: start;
    }
    
    
    /* Styling for the sections */
    
    section h2 {
      font-family: system-ui, sans-serif;
      font-size: 2rem;
      color: #1a1a1a;
      font-weight: 100;
    }
    
    section h3 {
      font-family: system-ui, sans-serif;
      font-size: 2rem;
      color: #ffffff;
      font-weight: 100;
    }
    
    .section-1 {
      background-image: url('https://images.unsplash.com/photo-1524260855046-f743b3cdad07?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1401&q=80')
    }
    
    .section-2 {
      background-image: url('https://images.unsplash.com/34/BA1yLjNnQCI1yisIZGEi_2013-07-16_1922_IMG_9873.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80')
    }
    
    .section-3 {
      background-image: url('https://images.unsplash.com/photo-1500964757637-c85e8a162699?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1378&q=80')
    }
    
    .section-4 {
      background-image: url('https://images.unsplash.com/photo-1501791330673-603715379ded?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')
    }
    <main class="slides">
      <section class="section-1">
        <h2>Scroll down to see it in action ?</h2>
      </section>
      <section class="section-2">
        <h3>Woah! That's a cool effect!</h3>
      </section>
      <section class="section-3">
        <h2>And no Javascript! ?</h2>
      </section>
      <section class="section-4">
        <h3>How cool is that?</h3>
      </section>
    </main>

    其他 css 属性仅用于美学,请随意忽略。

    【讨论】:

    • 滚动时是否可以捕捉?和我在笔中找到的一样,除了virtical。
    • 它已经这样做了,在小结果显示上可能不是很明显。检查Full page 中的答案(运行代码 sn-p 时出现在按钮旁边的小链接)
    • 我点击了整页,问题依旧。
    • 我明白你的意思了。它在 Firefox 中以您想要的方式工作,但在 Chrome 中却不行。让我检查一下。
    • Chrome 的行为已经在这里讨论过了 -> stackoverflow.com/questions/56629124/…
    猜你喜欢
    • 2011-12-06
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多