【问题标题】:Show loading gif while image is loading加载图像时显示加载 gif
【发布时间】:2015-06-26 20:39:59
【问题描述】:

我编写了一些代码,用户可以通过 zip 上传一些图片。在下一页上,我需要在 85*85 像素的框架中单独显示所有图像。

问题是加载所有图像可能需要一些时间。 所以我想在用户等待图片加载时显示加载 gif。

我已将图像的 src 设置为the loading gifs,同时我创建了一些以真实来源为 id 的复选框

echo "<td><img src=\"beelden/ajax-loader-black-16.png\" id=\"img".$image."\" style=\" width: 85px; height: 85px; border: 1px solid gray; background-color: #fff; padding: 10px;\">";
echo "<input type=\"checkbox\" id=\"img[".$image."]\" name=\"check_image[]\" value=\"".$filename."\" /></td>";
<input type="hidden" name="aantal" id="aantal" value="<?=$image?>" >

然后我创建了一些 javascript 来检查图像是否已加载,当加载时,它应该替换图像的来源。

<script>
    var aantal = document.getElementById("aantal").value;
    for(var i = 0; i < aantal; i++){
        var source = document.getElementById("img["+i+"]").value;
        var img = new Image();
        img.onload = function(){
            $("#img"+i).attr('src', source);
        }();
        img.src = source;
    }
</script>

但这并不像我预期的那样工作,我认为一旦加载第一个图像,它就会为所有图像触发。任何想法我做错了什么或如何解决这个问题?

【问题讨论】:

  • 你如何调用你的javascript函数??
  • 闭包下循环变量的经典案例。
  • 为快速编辑和演示创建一个小提琴
  • 你能创造一个小提琴或一个plunker吗?你写 `document.getElementById("img["+i+"]").value;` 和 $("#img"+i). 好混乱
  • 考虑不要使用闭包,因为在这种情况下肯定会导致 DOM 节点上的内存泄漏。声明一个将i 作为参数的外部函数,然后在每个循环中调用它。

标签: javascript jquery image


【解决方案1】:

您可以将图像的背景设置为加载 gif。这是一个简单的 CSS 技巧。那时你就不需要制作 js 脚本了。

.loading {
  background: transparent url('http://thinkfuture.com/wp-content/uploads/2013/10/loading_spinner.gif') center no-repeat;
}
<img class="loading" src="http://placehold.it/106&text=1" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=2" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=3" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=4" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=5" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=6" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=7" width="106px" height="106px" />

更新:

如果您有透明图像,那么故事会变得有点复杂,但仍然可以使用 css 和一些 div 元素来完成。

.image-wrapper {
  overflow: hidden;
  width: 106px;
  height: 106px;
  display: inline-block;
}

.image-wrapper img {
  float: left;
  display: block;
  opacity: 0.2; /* simulating a semitransparent image */
}

.image-wrapper:after, .loading {
  content: ' ';
  background: transparent url('http://thinkfuture.com/wp-content/uploads/2013/10/loading_spinner.gif')  center no-repeat ;
  background-size : auto 100%;
  width: 106px;
  height: 106px;
  float: left;
  display: block;
}
<div class="image-wrapper">
  <!-- simulates a hard loading image -->
  <img src="http://placehold.it/not-existing" alt="" />
</div>
<div class="image-wrapper">
  <img src="http://placehold.it/106x106&text=2" alt="" />
</div>
<div class="image-wrapper">
  <img src="http://placehold.it/106x106&text=3" alt="" />
</div>
<div class="image-wrapper">
  <img src="http://placehold.it/106x106&text=4" alt="" />
</div>
<div class="image-wrapper">
  <img src="http://placehold.it/106x106&text=5"  alt="" />
</div>
<div class="image-wrapper">
  <img src="http://placehold.it/106x106&text=6"  alt="" />
</div>
<div class="image-wrapper">
  <img src="http://placehold.it/106x106&text=7"  alt="" />
</div>

不幸的是,浏览器在加载时添加了一个损坏的图标或?,这就是为什么图像包含一个空的alt;

更新 2:

第二个变体非常依赖图像大小,如果您的大小与加载 gif 不同,则无法正确推开,作为替代方案是使用第一个变体和一个小 js 脚本,该脚本将删除加载图像后的背景:

$('img').load(function(){
   $(this).css('background','none');
});
   .loading {
      background: transparent url('http://thinkfuture.com/wp-content/uploads/2013/10/loading_spinner.gif') center no-repeat;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img class="loading" src="http://upload.wikimedia.org/wikipedia/en/2/2d/SRU-Logo-Transparent.png" width="106px" height="106px" />
    <img class="loading" src="http://placehold.it/106&text=2" width="106px" height="106px" />
    <img class="loading" src="http://placehold.it/106&text=3" width="106px" height="106px" />
    <img class="loading" src="http://placehold.it/106&text=4" width="106px" height="106px" />
    <img class="loading" src="http://placehold.it/106&text=5" width="106px" height="106px" />
    <img class="loading" src="http://placehold.it/106&text=6" width="106px" height="106px" />
    <img class="loading" src="http://placehold.it/106&text=7" width="106px" height="106px" />

【讨论】:

  • 如果原始图像也是透明的,这是否有效?
  • 不,不像我写的那样。作为替代方案,您可以创建一些包装器/帮助器,在加载图像时将加载 gif 推开,我将更新我的答案
  • 我将接受这是一个可能的正确答案,我只是用 javascript/jquery 修复了它,但我认为这是一个有效的替代方案 :)
  • 在什么浏览器上?
  • 如果加载的图片是背景精灵怎么办(ಠ_ಠ)
【解决方案2】:

带有 Hostlistener 和 Hostbinding 的 Angular 8 解决方案。

1.创建一个简单的属性指令

import { Directive, HostListener, Input, HostBinding } from '@angular/core';
@Directive({selector: '[imageLoader]'})
export class ImageLoaderDirective
{
  @Input('src') imageSrc;
  @HostListener('load')
  loadImage()
  {
  this.srcAttr=this.imageSrc;
  }

@HostBinding('attr.src') srcAttr="../../assets/pics/Loader.svg"
constructor() { }
}

基本上我们将初始图像源设置为加载器图像。一旦图像加载(加载事件触发),我们就会监听它并将图像源设置为实际图像。

2。现在使用刚刚需要在图像上使用创建的属性。

<img imageLoader src='image.jpg'>

使用 svg 不需要更改样式,gif 可能需要更改 css。

您可以访问website 进行工作实施。

【讨论】:

  • 你能提供完整的指令代码吗?谢谢
  • @fromage9747 我已经更新了答案本身的代码。如果有帮助,请点赞。
  • 谢谢!我似乎无法找到一种方法来测试它以查看它是否有效。即使在 chrome 中将我的网络速度设置为慢 3g,它也不会显示加载程序。
  • @fromage9747 它可能与您尝试显示的图像尺寸有关,但不确定。作为参考,我有这个网站-valleyviewhomestays.firebaseapp.com。对我来说,加载程序显示(但我的图像 jpg 以 MB 为单位)。我建议您使用具有更高分辨率和尺寸的图像的加载器进行测试。
  • 虽然这个问题没有明确要求 Angular 解决方案,但我很高兴在这里找到一个。我认为您的回答是解决问题的 Angular 方法的一个很好的基准。谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-06-05
  • 2014-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多