简体   繁体   English

CSS 仅当全屏视口滚动到时滚动捕捉

[英]CSS scroll snapping only when full screen viewport is scrolled to

I've successfully implemented the CSS scroll snap.我已经成功实现了 CSS 滚动捕捉。 However, I only applied it to some sections in the middle of my page.但是,我只将它应用到页面中间的某些部分。 When you start scrolling from the top and a bit of the snapping sections already show, when you continue scrolling, the snapping already begins.当您从顶部开始滚动并且一些捕捉部分已经显示时,当您继续滚动时,捕捉已经开始。

Is there a way for the scroll snap to begin only when the first section occupies the entire viewport?有没有办法让滚动捕捉仅在第一部分占据整个视口时才开始? And the last, when you scroll from the bottom?最后,当你从底部滚动时?

Thank you!谢谢!

The code I've tried is below:我试过的代码如下:

<div class="sticky-container">
    <div class="sticky-section">
        <h1>Slide One</h1>
    </div>
    <div class="sticky-section">
        <h1>Slide Two</h1>
    </div>
    <div class="sticky-section">
        <h1>Slide Three</h1>
    </div>
</div>
.sticky-container {
    height: 100%;
    width: 100%;
    scroll-snap-type: y mandatory;
    overflow-y: scroll;
}

.sticky-section {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100vw;
    height: 100vh;
    scroll-snap-align: start;
}

I think this is what you are looking for.我想这就是你要找的。

 body { margin: 0; }.sticky-container { height: 100vh; width: 100vw; scroll-snap-type: y mandatory; overflow-y: scroll; }.sticky-section { display: flex; justify-content: center; align-items: center; width: 100%; height: 100%; scroll-snap-align: start; }
 <div class="sticky-container"> <div class="sticky-section"> <h1>Slide One</h1> </div> <div class="sticky-section"> <h1>Slide Two</h1> </div> <div class="sticky-section"> <h1>Slide Three</h1> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM