简体   繁体   English

等待图像加载到下划线模板中

[英]Waiting for images to load in underscore template

I'm loading images inside underscore. 我正在下划线内加载图像。 I have a class in my container which has a loader image and it will disappear once my images are loaded. 我的容器中有一个包含加载程序映像的类,一旦加载映像,它将消失。

<div id="main_container" class="loading">
<% for (i = 0; i <= pages; i++) { %>
      <img src="/image_<%=i%>.jpg" id="image_<%=i%>" />
      <%
          $('#image_'+i).load(function() {
              $('#main_container').removeClass('loading');
          }).each(function() {
            if(this.complete) $(this).load();
          });
      %>
<% } %>
</div>

I can't make it work maybe because the dom is not yet created. 我无法使其正常工作,可能是因为尚未创建dom。 Is there a way for me to load the images in underscore template and remove the class loading when the images is completely loaded? 有没有办法在下划线模板中加载图像并在图像完全加载后删除类加载?

Thanks and more power 感谢和更多的力量

Just render your template with the class loading on your image and add the callback onload 只需在imageloading类的情况下渲染模板,然后添加回调onload

<% for (i = 0; i <= pages; i++) { %>
    <img src="/image_<%=i%>.jpg" class='loading' onload='hideLoading(this)' />
<% } %>

In your scripts file, add the callback 在脚本文件中,添加回调

function hideLoading(img) {
    $(img).removeClass('loading');
}

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

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