简体   繁体   English

样式加载事件后的 LitElement 阴影根

[英]LitElement shadow root after styles loaded event

I have a problem, I need to handle event after CSS styles was loaded into my component.我有一个问题,我需要在 CSS 样式加载到我的组件后处理事件。 I was trying to use lit-element lifecycle, still they are fired to early.我试图使用 lit-element 生命周期,但它们仍然很早就被解雇了。

I'm trying to get my DOM element starting position and other data, but I get an initial position where after styles is loaded this position changes?我正在尝试获取我的 DOM 元素起始位置和其他数据,但是我得到了一个初始位置,在加载样式后此位置会发生变化吗?

How to make styles load first or any event after styles loaded?如何在样式加载后首先加载样式或任何事件?

The firstUpdated lifecycle is typically what you're looking for: firstUpdated生命周期通常是您正在寻找的:

Called after the element's DOM has been updated the first time, immediately before updated is called.在第一次更新元素的 DOM 之后调用,即在调用更新之前。 Implement firstUpdated to perform one-time work after the element's template has been created.实现 firstUpdated 以在创建元素模板后执行一次性工作。

If your elements are still loading even after firstUpdated is fired, you could wait for another cycle just to be safe with:如果您的元素即使在firstUpdated被触发后仍在加载,您可以等待另一个循环以确保安全:

setTimeout(() => { /* code for checking DOM styles */ }, timeInMilliseconds);

timeInMilliseconds will likely only need to be 0 , but you may need to give it a bit more time because your styles are changing outside of typical styles. timeInMilliseconds可能只需要为0 ,但您可能需要给它多一点时间,因为您的样式正在改变典型样式之外。 This could also be the case if you have any CSS animation or transition that could be impacting the display of your elements.如果您有任何可能影响元素显示的 CSS animationtransition ,也可能是这种情况。

If all the above looks correct, I recommend opening an issue on the lit-element github page as it's still actively contributed to.如果以上所有内容看起来都正确,我建议在lit-element github 页面上打开一个问题,因为它仍在积极参与。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM