在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
最近研究CSS的时候发现了 仅使用两个CSS属性就可以制作出全屏滚动效果 ,这两个属性就是:
使用它就可以实现 今天说的这两个属性并不能替代
1. 兼容性目前主流的浏览器都已经支持了这两个CSS属性,可以放心的使用。如果你需要兼容IE浏览器,那么请选择 2. 使用使用的方法其实很简单, 整个网页的完整代码很简单,下面直接将它贴上来: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>CSS scroll snap</title> <style> body { margin: 0; } .container { height: 100vh; overflow-y: scroll; /* 在父容器上面使用 scroll-snap-type 属性 */ scroll-snap-type: y mandatory; } section { padding: 112px; height: calc(100vh - 224px); color: white; /* 在需要滚动的容器上使用 scroll-snap-align 属性 */ scroll-snap-align: start; } section:nth-of-type(1) { background-color: #60af15; } section:nth-of-type(2) { background-color: #158baf; } section:nth-of-type(3) { background-color: #af1581; } section h3 { font-size: 48px; } section p { font-size: 20px; } </style> </head> <body> <div class="container"> <section> <h3>A subtitle lives here</h3> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus deleniti dignissimos ducimus expedita iure maxime qui rerum veniam voluptatibus. Accusamus asperiores assumenda atque consectetur consequuntur culpa cum deserunt dicta distinctio error excepturi fuga ipsa iste magnam modi nobis, obcaecati, pariatur perspiciatis placeat quo quod reiciendis repudiandae saepe soluta tempora unde vel? Aliquam exercitationem iste maiores placeat reprehenderit voluptates voluptatum. Ad at commodi culpa cumque debitis delectus dolorum, eius error et explicabo harum in ipsum iste labore laborum libero magni maiores nam non nostrum nulla officia pariatur quam quasi quia quo recusandae reprehenderit saepe similique vel vero vitae voluptas voluptatem! Quibusdam. </p> </section> <section> <h3>A subtitle lives here</h3> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus deleniti dignissimos ducimus expedita iure maxime qui rerum veniam voluptatibus. Accusamus asperiores assumenda atque consectetur consequuntur culpa cum deserunt dicta distinctio error excepturi fuga ipsa iste magnam modi nobis, obcaecati, pariatur perspiciatis placeat quo quod reiciendis repudiandae saepe soluta tempora unde vel? Aliquam exercitationem iste maiores placeat reprehenderit voluptates voluptatum. Ad at commodi culpa cumque debitis delectus dolorum, eius error et explicabo harum in ipsum iste labore laborum libero magni maiores nam non nostrum nulla officia pariatur quam quasi quia quo recusandae reprehenderit saepe similique vel vero vitae voluptas voluptatem! Quibusdam. </p> </section> <section> <h3>A subtitle lives here</h3> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus deleniti dignissimos ducimus expedita iure maxime qui rerum veniam voluptatibus. Accusamus asperiores assumenda atque consectetur consequuntur culpa cum deserunt dicta distinctio error excepturi fuga ipsa iste magnam modi nobis, obcaecati, pariatur perspiciatis placeat quo quod reiciendis repudiandae saepe soluta tempora unde vel? Aliquam exercitationem iste maiores placeat reprehenderit voluptates voluptatum. Ad at commodi culpa cumque debitis delectus dolorum, eius error et explicabo harum in ipsum iste labore laborum libero magni maiores nam non nostrum nulla officia pariatur quam quasi quia quo recusandae reprehenderit saepe similique vel vero vitae voluptas voluptatem! Quibusdam. </p> </section> </div> </body> </html> 可以看到代码并不复杂,下面我们就着重讲解一下这两个CSS属性。 3. scroll-snap-type该CSS属性拥有下面这些值: none:当这个滚动容器的可视的 viewport 是滚动的,不做任何处理。
其中需要注意的就是上面粗体标注的几个属性,使用 而 理解这两个属性其实非常简单,将上面的代码改改自己体验下就明白了。 注:使用 4. scroll-snap-align该CSS属性拥有下面这些值: none:该容器不会进行定义在父容器上面对应轴的捕捉。 start:该容器被捕捉的位置是该容器开始的部分。 end:该容器被捕捉的位置是该容器结束的部分。 center:该容器被捕捉的位置是该容器中间的部分。 用一张图可以很形象的明白这些属性所代表的容器位置: 5. 最后因为我看到 参考文章: 到此这篇关于CSS实现fullpage.js全屏滚动效果的示例代码的文章就介绍到这了,更多相关CSS全屏滚动内容请搜索极客世界以前的文章或继续浏览下面的相关文章,希望大家以后多多支持极客世界! |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论