简体   繁体   English

我可以在所有元素上使用内容可见性吗?

[英]Can I use content-visibility on all elements?

So, with content-visibility improving the page load time by skipping content that are not in the viewport, is there a real reason preventing its usage across all DOM elements?那么,内容可见性通过跳过不在视口中的内容来缩短页面加载时间,是否有真正的原因阻止其在所有 DOM 元素中使用?

Rather than use而不是使用

.card {
    content-visibility: auto;
}

Why not use为什么不使用

* {
    content-visibility: auto;
}

I think the short answer is that you would loose a little of the performance benefits of 'content-visibility'.我认为简短的回答是你会失去一些“内容可见性”的性能优势。

Why?为什么?

as they say, "There ain't no such thing as a free lunch" , so there is always a trade of.正如他们所说, “世上没有免费的午餐” ,所以总是有交易。 content-visibility improves performance by not rendering parts of the dom that are not on screen (info) , so it will firts validate if the part needs to be rendered or not, and then perform the rendering process. content-visibility 通过不渲染不在屏幕上的部分(信息)来提高性能,因此它将首先验证该部分是否需要渲染,然后执行渲染过程。

So you are adding some processing (the validation) and removing other (the rendering).因此,您正在添加一些处理(验证)并删除其他处理(渲染)。 You are gonna have performance improvement as long as what you are removing is greater than what you are adding.只要您删除的内容大于添加的内容,您就会获得性能提升。

If you use something like如果你使用类似的东西

* {
    content-visibility: auto;
}

You are basically telling the browser to validate everything before rendering.您基本上是在告诉浏览器在渲染之前验证所有内容。 This means that the nodes, and their children recursively will be validated.这意味着将递归地验证节点及其子节点。

Let me do a brief extremely over simplificated and over exagerated example to explain what I mean (numbers here are not real at all)让我做一个非常简单和夸张的例子来解释我的意思(这里的数字根本不是真的)

Lets say that you have a screen with three.card of which 1 and a half are visible initially, each has 10 children.假设您有一个带有 3.card 的屏幕,其中 1 个半最初是可见的,每个有 10 个孩子。

在此处输入图像描述

Lets say and each card renders in 1 second and each node validation takes lets say 100ms.假设每张卡在 1 秒内呈现,每个节点验证需要 100 毫秒。

So currently the render time would be 3 seconds所以目前渲染时间为 3 秒

Applying content-visibility: auto;应用content-visibility: auto; to the .card would result in 3 validations (+300ms) and one of the cards would not be rendered (-1 second) so the total render time would be 2.3 seconds.card将导致 3 次验证(+300 毫秒)并且其中一张卡不会被渲染(-1 秒),因此总渲染时间为 2.3 秒

Applying content-visibility: auto;应用content-visibility: auto; to * would result in 30 validations (+3000ms) and one and a half of the cards would not be rendered (-1.5 seconds) so the total render time would be 4.5 seconds*将导致 30 次验证(+3000 毫秒),并且一张半的卡片不会被渲染(-1.5 秒),因此总渲染时间为 4.5 秒

So content-visibility works best for blocks of elements that are expensive to render on the inside.因此, content-visibility最适合在内部渲染成本高昂的元素块。

Its possible that the browsers will implement some optimizations in the future;未来浏览器可能会进行一些优化; I have not found information about optimizations, but for now as the feature is quite new, using it like that may cause issues.我还没有找到有关优化的信息,但目前由于该功能非常新,因此使用它可能会导致问题。

The main point of using content-visibility is performance.使用内容可见性的要点是性能。 It can help to speed up page load because the browser is able to defer rendering elements that are not in the user's viewport until the user scrolls to them.它可以帮助加快页面加载,因为浏览器能够推迟呈现不在用户视口中的元素,直到用户滚动到它们。 By using this,通过使用这个,

* {
    content-visibility: auto;
}

You are basically telling the browser to validate everything before rendering.您基本上是在告诉浏览器在渲染之前验证所有内容。 This means that the nodes, and their children recursively will be validated.这意味着将递归地验证节点及其子节点。 If you use a content-visibility for a particular section or element,如果您对特定部分或元素使用内容可见性,

section{
content-visibility:auto;
}

The browser only renders that particular element or section.浏览器仅呈现该特定元素或部分。 it will saves the browser's rendering time.它将节省浏览器的渲染时间。

暂无
暂无

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

相关问题 在用户滚动到它们之前触发所有 `content-visibility: auto` 元素的渲染,而不会阻塞主线程超过 50 毫秒 - Trigger rendering of all `content-visibility: auto` elements BEFORE a user scrolls to them without blocking main thread for >50ms 为什么 content-visibility:auto 在这个简单的例子中不起作用? - Why isn't content-visibility:auto working in this simple example? CSS 新的“内容可见性”属性是否会干扰脚本加载行为? - Does CSS new 'content-visibility' property interfere with scripts loading behavior? 我可以使用div:before的内容作为锚来切换div其余部分的可见性吗? - Can I use the content of div:before as an anchor to toggle visibility of the rest of the div? 切换所有人的可见性 <pre> 元素 - Toggle visibility of all <pre> elements 如何更改屏幕上未显示的元素的CSS可见性样式? - How can I change the CSS visibility style for elements that are not on the screen? 我可以根据一天中的时间对 HTML 中的内容可见性进行计时吗? - Can I time content visibility in HTML based on time of day? 为什么我不能使用 javascript 将可见性属性更改为隐藏 - Why can't I use javascript to change the visibility property to hidden 在Tritium中,如何根据现有元素的内容插入元素? - In Tritium, how can I insert elements based on content of existing elements? 在页脚上使用slidetoggle时,如何从顶部推入所有元素? - How can I push all elements from the top when I use slidetoggle on the footer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM