简体   繁体   English

是否有一个普通的普通JS / HTML(或JQuery)事件在元素开始/结束渲染时触发?

[英]Is there a normal vanilla JS/HTML (or JQuery) event that fires when an element begins/finishes rendering?

I'd like something similar to the document.ready event, but for any DOM element. 我想要类似于document.ready事件,但适用于任何DOM元素。

In more details, I'm hacking away at an app which has a custom framework. 更详细地讲,我正在破解具有自定义框架的应用程序。 I'm trying to add a loading screen. 我正在尝试添加加载屏幕。 This screen should disappear whenever a certain element pops up on the screen. 每当屏幕上弹出某个元素时,该屏幕就会消失。

Now I don't really know how I'd do this in a clean way, or what a recommended way of doing such things would be, but at the moment, for me it would be enough if I could implement this pseudocode 现在我真的不知道如何以一种干净的方式进行此操作,或者建议采用哪种方式进行此类操作,但就目前而言,如果我可以实现此伪代码就足够了

container.show_loading_spinner()
// .. other stuff ...
// this should only trigger when the next line, which starts creating elements begins
container.on_drawing_begin(function() 
    {container.hide_loading_spinner()})

//when this begins drawing, the spinner should get hidden.
container.create_some_elements()

I realise this is probably quite complex, and kind of hope I won't be disregarded as a noob but I'm mainly focused on backend development. 我意识到这可能非常复杂,并且希望我不会被视为菜鸟,但我主要专注于后端开发。 Is this thing that I'm asking even possible, or should I think this through in a completely different way? 我要问的这个事情是否可能,还是我应该以一种完全不同的方式来思考?

[EDIT] [编辑]

Q: What is my definition of "rendering"? 问:我对“渲染”的定义是什么?

A: The moment when an element is present in the DOM and visible on the screen, I call it "rendered". 答:当元素出现在DOM中并在屏幕上可见时,我称其为“渲染”。 The moment when it's not in the DOM, it's unrendered. 它不在DOM中的那一刻,它是未渲染的。 Rendering is the process which takes the DOM (and corresponding screen - if the element is visible) from the first state to the second. 渲染是将DOM(以及相应的屏幕-如果该元素可见)从第一状态进入第二状态的过程。

More casually though I just want "some event" which is triggered very close in time to the moment when an element begins to render. 更随意地说,尽管我只想“某个事件”,它在元素开始渲染的那一刻非常接近地触发。 I need the beginning, because that's when I want to turn off the spinner. 我需要开始,因为那是我想关闭微调器的时间。 I don't want the spinner to be present on the screen at the same time as my elements. 我不希望微调器与我的元素同时出现在屏幕上。

What if you trigger a custom event like hideSpinner when you start rendering your elements? 如果在开始渲染元素时触发了诸如hideSpinner类的自定义事件, hideSpinner怎么办?

https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events https://developer.mozilla.org/zh-CN/docs/Web/Guide/Events/Creating_and_triggering_events

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

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