简体   繁体   English

显示加载动画,直到出现 <div> 出现?

[英]Display a loading animation until a <div> appears?

I'm running an asynchronous 3rd party script that loads an image gallery into my page, but unfortunately their code doesn't provide me with a callback after their image gallery has finished loading. 我正在运行一个异步的3rd方脚本,该脚本将图像库加载到我的页面中,但是不幸的是,他们的代码在图像库加载完成后没有向我提供回调。

The modal starts off like this: 模态开始像这样:

<div class="modal-body">
  <div class="container-fluid" id="cincopa">

  </div>
</div>

After the gallery is loaded, the modal looks like this: 加载画廊后,模态如下所示:

<div class="modal-body">
  <div class="container-fluid" id="cincopa">
    <div id="ze_galleria">
       //gallery stuff
    </div>
  </div>
</div>

So I need some way to display a loading animation until #ze_galleria appears. 因此,我需要某种方法来显示加载动画,直到#ze_galleria出现为止。 The loading animation function I can do myself, but is there something in jQuery that will listen for when a certain DOM element is created? 我可以自己执行加载动画功能,但是jQuery中是否有某些东西可以在创建某个DOM元素时侦听? Once the DOM element appears, it'll run the callback to remove the animation. 出现DOM元素后,它将运行回调以删除动画。

Based on how that script adds the gallery/gallery items you could use the DOMSubtreeModified event and then check if that particular item were added 根据该脚本如何添加画廊/画廊项目,您可以使用DOMSubtreeModified事件,然后检查是否添加了该特定项目。

document.addEventListener("DOMSubtreeModified", function(e) {

    if (document.querySelector("#ze_galleria")) {

        // exists

    }

});

Here is a DOM tree event list, where you can check other possible events that might could be used. 这是DOM树事件列表,您可以在其中检查其他可能使用的事件。

Update 更新资料

Make sure you take a look at the MutationObserver as well, as it has very good browser support nowadays. 确保您也了解一下MutationObserver ,因为当今它具有很好的浏览器支持。

Also, you can set an interval: 另外,您可以设置一个时间间隔:

var cincopa = $('#cincopa');
var counter = 0;
var intervalCode = setInterval(function(){
    if (cincopa.find('#ze_galleria').length){
        clearInterval(intervalCode);
        yourCallback();
    }
    counter++;
    console.log(counter + ' seconds past till the event loaded.');
}, 1000);

I think the code is intuitive, but if there is any doubt, just ask :) 我认为代码很直观,但是如果有任何疑问,请问:)

Presuming that your "3rd party library" is going to totally overwrite the contents of what ever you point it at (as your example code suggests it does). 假设您的“第3方库”将完全覆盖您指向的内容(如示例代码所建议的那样)。 You can solve your problem simply by adding an img: 您只需添加img即可解决您的问题:

<div class="modal-body">
   <div class="container-fluid" id="cincopa">
     <img src="loading.gif"/>
   </div>
</div>

When the library has done what it needs to do it will overwrite the contents of the div <div class="container-fluid" id="cincopa"> resulting in: 库完成所需的操作后,它将覆盖div <div class="container-fluid" id="cincopa">从而导致:

<div class="modal-body">
  <div class="container-fluid" id="cincopa">
    <div id="ze_galleria">
       //gallery stuff
    </div>
  </div>
</div>

thus removing your loading image. 从而删除您的加载图片。

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

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