From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../guide_to_scroll_anchoring/index.html | 92 ++++++++++++++++++++++ files/zh-cn/web/css/overflow-anchor/index.html | 87 ++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 files/zh-cn/web/css/overflow-anchor/guide_to_scroll_anchoring/index.html create mode 100644 files/zh-cn/web/css/overflow-anchor/index.html (limited to 'files/zh-cn/web/css/overflow-anchor') diff --git a/files/zh-cn/web/css/overflow-anchor/guide_to_scroll_anchoring/index.html b/files/zh-cn/web/css/overflow-anchor/guide_to_scroll_anchoring/index.html new file mode 100644 index 0000000000..6a0f3b43f8 --- /dev/null +++ b/files/zh-cn/web/css/overflow-anchor/guide_to_scroll_anchoring/index.html @@ -0,0 +1,92 @@ +--- +title: Guide to scroll anchoring +slug: Web/CSS/overflow-anchor/Guide_to_scroll_anchoring +translation_of: Web/CSS/overflow-anchor/Guide_to_scroll_anchoring +--- +
{{CSSRef}}
+ +

As a user of the web, you are probably familiar with the problem that scroll anchoring solves. You browse to a long page on a slow connection and begin to scroll to read the content; while you are busy reading, the part of the page you are looking at suddenly jumps. This has happened because large images or some other elements have just loaded further up in the content.

+ +

Scroll anchoring is a browser feature that aims to solve this problem of content jumping, which happens if content loads in after the user has already scrolled to a new part of the document.

+ +

How does it work?

+ +
+

Scroll anchoring adjusts the scroll position to compensate for the changes outside of the viewport. This means that the point in the document the user is looking at remains in the viewport, which may mean their scroll position actually changes in terms of how far they have moved through the document.

+
+ +

How do I turn on scroll anchoring?

+ +

You don't! The feature is enabled by default in supporting browsers. In most cases anchored scrolling is exactly what you want — content jumping is a poor experience for anyone.

+ +

What if I need to debug it?

+ +

If your page is not behaving well with scroll anchoring enabled, it is probably because some scroll event listener is not handling well the extra scrolling to compensate for the anchor node movement.

+ +

You can check whether disabling scroll anchoring fixes the issue in Firefox by changing layout.css.scroll-anchoring.enabled to false in about:config.

+ +

If it does, you can check what node is Firefox using as the anchor using the layout.css.scroll-anchoring.highlight switch. That will show a purple overlay on top of the anchor node.

+ +

If one node doesn't seem appropriate to be an anchor, you can exclude it using {{cssxref("overflow-anchor")}}, as described below.

+ +

What if I need to disable it?

+ +

The specification provides a new property, {{cssxref("overflow-anchor")}}, which can be used to disable scroll anchoring on all or part of the document. It's essentially a way to opt out of the new behavior.

+ +

The only possible values are auto or none:

+ + + +

To opt out the entire document, you can set it on the {{htmlelement("body")}} element:

+ +
body {
+  overflow-anchor: none;
+}
+ +

To opt out a certain part of the document use overflow-anchor: none on its container element:

+ +
.container {
+  overflow-anchor: none;
+}
+ +
+

Note: The specification details that once scroll anchoring has been opted out of, you cannot opt back into it from a child element. For example, if you opt out for the entire document, you will not be able to set overflow-anchor: auto elsewhere in the document to turn it back on for a subsection.

+
+ +

Suppression triggers

+ +

The specification also details some suppression triggers, which will disable scroll anchoring in places where it might be problematic. If any of the triggers happen on the anchor node, or an ancestor of it, anchoring is suppressed.

+ +

These suppression triggers are changes to the computed value of any of the following properties:

+ + + +

Additionally, {{cssxref("position")}} changes anywhere inside the scrolling box also disable scroll anchoring.

+ +
+

In {{bug(1584285)}} the layout.css.scroll-anchoring.suppressions.enabled flag was added to Firefox Nightly in order to allow the disabling of these triggers.

+
+ +

Further reading

+ + + +

Browser compatibility

+ +

If you need to test whether scroll anchoring is available in a browser, use Feature Queries to test support for the overflow-anchor property.

+ + + +

{{Compat("css.properties.overflow-anchor")}}

diff --git a/files/zh-cn/web/css/overflow-anchor/index.html b/files/zh-cn/web/css/overflow-anchor/index.html new file mode 100644 index 0000000000..42150355bf --- /dev/null +++ b/files/zh-cn/web/css/overflow-anchor/index.html @@ -0,0 +1,87 @@ +--- +title: overflow-anchor +slug: Web/CSS/overflow-anchor +tags: + - CSS + - CSS Scroll Anchoring +translation_of: Web/CSS/overflow-anchor +--- +
{{CSSRef}}
+ +

overflow-anchor CSS 属性提供一种退出浏览器滚动锚定行为的方法,该行为会调整滚动位置以最大程度地减少内容偏移。

+ +

默认情况下,在任何支持滚动锚定行为的浏览器中都将其启用。因此,仅当您在文档或文档的一部分中遇到滚动锚定问题并且需要关闭行为时,才通常需要更改此属性的值。

+ +

语法

+ +
/* Keyword values */
+overflow-anchor: auto;
+overflow-anchor: none;
+
+/* Global values */
+overflow-anchor: inherit;
+overflow-anchor: initial;
+overflow-anchor: unset;
+
+ +

Values

+ +
+
auto
+
The element becomes a potential anchor when adjusting scroll position.
+
none
+
The element won't be selected as a potential anchor.
+
+ +

Formal syntax

+ +
{{csssyntax}}
+ +

范例

+ +

To prevent scroll anchoring in a document, use the overflow-anchor property.

+ +
body {
+  overflow-anchor: none;
+}
+
+ + + +

规范

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('CSS Scroll Anchoring', '#propdef-overflow-anchor', 'overflow-anchor')}}{{Spec2('CSS Scroll Anchoring')}}Initial definition.
+ +

{{cssinfo}}

+ +

浏览器兼容性

+ + + +

{{Compat("css.properties.overflow-anchor")}}

+ +

See also

+ + + +
+
+
-- cgit v1.2.3-54-g00ecf