CSS滚动捕捉,垂直不起作用

编程入门 行业动态 更新时间:2024-10-28 04:17:45
本文介绍了CSS滚动捕捉,垂直不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我将此文章用作参考点,具体来说就是这项工作代码段,但是在我的页面(以下脚本)中,垂直快照滚动不起作用。您知道为什么吗?

I used this article as point of reference, in specific this working snippet, but in my page (script below) the vertical snap scrolling isn't working. Do you have any idea why?

.parent { height: 100vh; scroll-snap-type: mandatory; scroll-snap-points-y: repeat(100vh); scroll-snap-type: y mandatory; } section { height: 100vh; scroll-snap-align: start; position: relative; } .one { background-color: red; } .two { background-color: blue; } .three { background-color: grey; } .four { background-color: green; }

<div class="parent row"> <section class="one"> </section> <section class="two"> </section> <section class="three"> </section> <section class="four"> </section> </div>

推荐答案

您在代码段中遇到的主要问题是,显示的滚动条属于正文,在该正文中未定义任何滚动快照属性。这就是为什么滚动时没有任何捕捉行为的原因。

The major problem you have in your code snippet, is that the displayed scrollbar belongs to the body, where no scroll-snap properties have been defined. This is why you do not have any snapping behaviour when scrolling.

要获得预期的结果,您需要

To achieve your expected result, you need to

  • 确保显示的滚动条属于父div
  • 将 overflow 行为定义为父容器以 scroll
  • Be sure that the displayed scrollbar belongs to the parent div
  • Define the overflow behaviour to the parent container to scroll
  • 在工作样本下

    请注意,请注意(对于chrome)的捕捉属性已经发展,并且您正在使用不赞成使用的功能。请参见 Google开发人员上的CSS滚动快照。

    As a note, consider that snapping properties (for chrome) have evolved, and that you are using deprecated features. See the css scroll snap on google developers.

    还请注意,此答案仅适用于铬,不涉及polyfill部分。仅仅是这里涉及的主要滚动概念

    Note also that this answer deals only with chrome, without the polyfill part. It is just the main scroll concept that is involved here

    html, body { height: 100vh; overflow: hidden; } .parent { overflow: scroll; height: 100vh; scroll-snap-type: mandatory; scroll-snap-points-y: repeat(100vh); scroll-snap-type: y mandatory; } section { height: 100vh; scroll-snap-align: start; position: relative; } .one { background-color: red; } .two { background-color: blue; } .three { background-color: grey; } .four { background-color: green; }

    <script src="ajax.googleapis/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="ajax.googleapis/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parent row"> <section class="one"> </section> <section class="two"> </section> <section class="three"> </section> <section class="four"> </section> </div>

    更多推荐

    CSS滚动捕捉,垂直不起作用

    本文发布于:2023-11-03 01:04:38,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1553864.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:不起作用   CSS

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!