CSS 性能问题。上课时口吃适用

发布于 2025-01-11 02:42:06 字数 678 浏览 0 评论 0 原文

我正在使用顺风类在我的网站上实现一个黑暗主题。例如:

由于某种原因,每当我改变主题时,过渡并不顺利,而是滞后和口吃。我注意到,随着页面上深色风格元素的增加,延迟变得更糟。

我不确定这实际上是浏览器中的顺风问题还是一般的 CSS 性能问题。

更新

经过一些测试,我得出的结论是,它实际上与过渡无关,而是与 CSS 本身有关。

我在页面上有很多元素最初设置了深色类,然后每当我更改主题时,我只需在 中添加/删除“深色”类。我的页面上可能有 50 个元素,这些元素会更改其文本颜色和背景颜色,并且每当主题更改时,所有这些元素都会出现延迟。这种滞后类似于内存泄漏或循环。它会断断续续,然后一次加载所有内容之类的东西。

所以我想我的问题是在这种情况下如何优化 CSS 性能?或整体?

更新2

根据开发工具中的性能页面,我在更改主题时丢失了很多帧。实际上感觉我的帧率达到了 5 fps。以下是性能页面 prnt.sc/Zz6T88ZFs6Fp 的屏幕截图。我不知道如何阅读它,也许有人可以提供一些有用的信息

如果有人曾经遇到/解决过这个问题,我将非常感谢你的建议

I'm implementing a dark theme on my website using tailwind classes. For example:
<tr className="bg-white dark:bg-dark-blue text-left dark:text-white transition-colors duration-300"></>

And for some reason, whenever I change my theme the transition isn't smooth but laggy and stuttering. I've noticed that with the increase of dark-styled elements on the page, the lag has gotten worse.

I'm not sure if this is actually a tailwind problem or a general CSS performance problem in the browser.

UPDATE

After some test I've come to the conclusion that it actually has nothing to do with transitions but with CSS itself.

I have a lot of elements on the page with dark classes set initially and then whenever I change the theme I just add/remove 'dark' class in <html>. I have may be 50 elements on the page which change their text color and background color and all of them are laggy whenever the theme gets changed. The lag is similar to when you have a memory leak or a loop. It stutters then loads all at once and stuff like that.

So I guess my question is how to optimize CSS performance in this case? or overall?

UPDATE 2

According to Performance page in devtools I'm dropping a lot of frames when changing the theme. And it actually feels like I'm getting 5fps. Here is a screenshot of Performance page prnt.sc/Zz6T88ZFs6Fp. I'm not sure how to read it, may be somebody can give some useful info

If anyone has ever faced/solved this problem, I'd greatly appreciate your advice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伴我心暖 2025-01-18 02:42:06

这可能与关键渲染路径有关。我建议您阅读这篇优秀的文章: 使用 CSS3 实现 60 FPS 动画
简而言之,如果您在 transformopacity css 属性上设置了 css 过渡,则浏览器必须重新计算整个布局,这会导致卡顿。

It may have to do with the critical rendering path. I'd advice you to read this excellent article : Achieving 60 FPS Animations with CSS3.
In a nutshell, if you have css transitions set other than on transform and opacity css properties, your browser has to recalculate the whole layout, which causes stuttering.

请帮我爱他 2025-01-18 02:42:06

我不使用顺风,并且在没有示例代码的情况下很难测试我的响应,但几年前我遇到了一个非常类似的问题,当时浏览器有点愚蠢。

我在整个 html 中有很多颜色类,并用 jquery 切换它们。这是一个简单的示例,说明了如何...

$(".red").removeClass("red").addClass("blue");

我当时确定,问题在于这些项目会导致每次更改都重新绘制。

我最终做的是在身体上交换一个类:

<body class="blue">
<a href="#">Whatever</a>
<a href="#">Something else</a>
<!-- lots more -->
</body>

然后:

body.blue a {
color: blue;
}
body.red a {
color: red;
}

这工作得很好,因为据我了解,重绘是一次性发生的......但我猜想甚至尝试你必须写很多东西都是你自己做的,而不是使用顺风。也许不会,所以我希望即使使用顺风,这种思路也能有所帮助。

编辑:如果我遇到同样的问题并且无法足够快地解决它,我会用快速不透明度淡出身体,然后再回来。完整的破解,但它可能看起来很酷。这不是解决您真正问题的方法,但如果您无法解决它,它可能会有所帮助。

I don't use tailwind and it's hard to test my response without example code but I had a very similar problem several years ago when browsers were a little dumb.

I had many classes for color throughout the html and toggled them with jquery. Here's a quick example of how...

$(".red").removeClass("red").addClass("blue");

I determined that, at the time, the problem was that the items were causing repainting for each change.

What I ended up doing was swapping a class on the body:

<body class="blue">
<a href="#">Whatever</a>
<a href="#">Something else</a>
<!-- lots more -->
</body>

And then:

body.blue a {
color: blue;
}
body.red a {
color: red;
}

And that worked beautifully because, as I understood it, the repaint happened all at once... but I'm guessing to even try you'd have to write a lot of stuff on your own instead of using tailwind. Maybe not, so I hope this line of thinking helps even if using tailwind.

Edit: if I had the same problem and couldn't fix it quickly enough I'd fade the body with a quick opacity out and then back in. Complete hack but it might look cool. Not a solution to your real problem but it might help if you can't fix it.

扮仙女 2025-01-18 02:42:06

您可以进入开发工具,然后进入“网络”,然后单击“禁用缓存”。

You can go in the dev tools and then go under "network", then you click on "Disable cache".

夜唯美灬不弃 2025-01-18 02:42:06

经过大量研究,我终于找到了一些有用的信息。
本文来自这里的一些信息 我发现丢帧实际上并不罕见,但通常不像我的情况那么严重。

Profile.json(从网络页面下载)中有一些特定的行,可以帮助您找到丢帧的实际原因。查找unsupportedProperties
就我而言是{"args":{"data":{"compositeFailed":8224,"unsupportedProperties":["color"]}},"cat":"blink.animations,devtools.timeline,benchmark,rail", “id2”:{“本地”: “0x76c803503df8”},“名称”:“动画”,“ph”:“n”,“pid”:40040,“范围”:“blink.animations,devtools.timeline,基准,rail”,“tid”:13280 ,“ts”:166257565931}。对我来说,原因是页面上的太多元素改变了它们的文本颜色。 884 行 compositeFailed 与我页面上一个主题切换的“颜色”相关。只有在 https://csstriggers.com/ 的帮助下进行重构才对我的情况有效。

目前,我不知道复合失败的实际原因,除了它与颜色属性和闪烁动画有关。欢迎任何 Google 开发人员启发我,因为我真的找不到任何相关信息。

After a lot of research I've managed to finally find some useful info.
With the help of this article and some info from here I found out that dropping frames is actually not uncommon but usually it is not as intense as in my case.

There are some particular lines in Profile.json (download from Network page), which can help you find the actual reason(s) of dropped frames. Look for unsupportedProperties.
In my case it was {"args":{"data":{"compositeFailed":8224,"unsupportedProperties":["color"]}},"cat":"blink.animations,devtools.timeline,benchmark,rail","id2":{"local":"0x76c803503df8"},"name":"Animation","ph":"n","pid":40040,"scope":"blink.animations,devtools.timeline,benchmark,rail","tid":13280,"ts":166257565931}. For me the reason was that too many elements on the page change their text color. 884 lines of compositeFailed related to 'color' for one theme switch on my page. Only refactor with the help of https://csstriggers.com/ would be effective in my case.

Currently, I don't know actual reasons for compositeFailed apart from that it has to do with color property and blink animation. Any Google devs would be welcome to enlighten me because I can't really find any info about that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文