【问题标题】:Timing a function to run after an image element has changed its src在图像元素更改其 src 后定时运行函数
【发布时间】:2014-07-02 15:54:48
【问题描述】:

我正在更改一些图像的 src 属性。加载新图像后,我想运行一个名为 wheelBuilder.rebuildColors() 的函数

$images.each(function() {
    $(this).attr("src", $(this).attr("data-original-front-src"));
});
wheelBuilder.rebuildColors();

此代码在某些浏览器(chrome、safari、ie)上运行良好,但在 Mozilla firefox 上不运行。

有没有办法设置它在所有图像加载后运行该功能?

谢谢

【问题讨论】:

标签: javascript jquery


【解决方案1】:

试试这个。更改src之前必须设置函数

var total = $images.length,loaded=0;
function testRebuild() {
  if (loaded==total) wheelBuilder.rebuildColors();
}
$images.each(function() {
  this.onload=function() {
    loaded++; testRebuild();
  }
  this.onerror=function() {
    $(this).hide();
    loaded++; testRebuild();
  }
  $(this).attr("src", $(this).attr("data-original-front-src"));
});

【讨论】:

  • 我应该提到其中一些图像不存在并且被错误处理程序隐藏,因此加载的变量永远不会达到总变量
猜你喜欢
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 2015-09-11
相关资源
最近更新 更多