- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个脚本,它应该在加载图像时显示文本“正在加载...”,然后在加载所有图像时将文本更改为“已加载”。我添加了一个按钮来加载新图像,以确保它也适用于动态加载的图像。
这在 Chrome 中完美运行,但在 Firefox 中,“正在加载...”文本永远不会出现。我不知道为什么会这样。页面开始加载,但并非所有图像都已加载,因此它应该创建文本“正在加载..”,但事实并非如此。然后,当所有图像加载完毕后,会出现文本“正在加载”。
我只是不明白为什么一条消息会出现而另一条消息不会出现。特别是因为在创建“正在加载...”文本之前不需要满足任何条件,所以它应该自动触发。
jsfiddle Example | Full Page Example
$(document).ready(function() {
var checkComplete = function() {
if($('img').filter(function() {return $('img').prop('complete');}).length == $('img').length) {
$('.status').text('Loaded');
} else {
$('.status').text('Loading...');
}
};
$('img').on('load',function() {
checkComplete();
});
$('#button').click(function() {
$('img.a').attr('src' , 'http://farm9.staticflickr.com/8545/8675107979_ee12611e6e_o.jpg');
$('img.b').attr( 'src' , 'http://farm9.staticflickr.com/8382/8677371836_651f586c99_o.jpg');
checkComplete();
});
checkComplete();
});
最佳答案
您的代码中有几个问题。
首先,checkComplete()
函数编写不正确。应该是这样的:
var checkComplete = function() {
var imgs = $('img');
if(imgs.filter(function() {return this.complete;}).length == imgs.length) {
$('.status').text('Loaded');
} else {
$('.status').text('Loading...');
}
};
这里的主要修复是过滤器回调需要引用 this.complete
,而不是 $('img').prop('complete')
因为您正在尝试一次过滤单个项目。
其次,在更改 .src
值后,您将依赖 .complete
和 .load
正常工作。这显然是它们无法在所有浏览器中正常工作的情况之一。
解决此问题的万无一失的方法是为新图像创建一个新图像对象,在设置 .src
值之前设置 onload 处理程序,当两个 onload 处理程序都触发时,您将知道两个新图像都已加载,您可以将 DOM 中的图像替换为新图像。
这是在 FF 中运行的版本:
$(document).ready(function() {
$('#button').click(function() {
var imgA = new Image();
var imgB = new Image();
imgA.className = "a";
imgB.className = "b";
var loaded = 0;
imgA.onload = imgB.onload = function() {
++loaded;
if (loaded == 2) {
$("img.a").replaceWith(imgA);
$("img.b").replaceWith(imgB);
$('.status').text('Loaded');
}
}
// the part with adding now to the end of the URL here is just for testing purposes to break the cache
// remove that part for deployment
var now = new Date().getTime();
imgA.src = 'http://farm9.staticflickr.com/8545/8675107979_ee12611e6e_o.jpg?' + now;
imgB.src = 'http://farm9.staticflickr.com/8382/8677371836_651f586c99_o.jpg?' + now;
$('.status').text('Loading...');
});
});
工作演示:http://jsfiddle.net/jfriend00/yy7GX/
<小时/>如果您想保留原始对象,您可以仅使用新创建的对象来预加载新图像,然后在预加载后更改 .src,如下所示:
$(document).ready(function() {
$('#button').click(function() {
var imgA = new Image();
var imgB = new Image();
var loaded = 0;
imgA.onload = imgB.onload = function() {
++loaded;
if (loaded == 2) {
$("img.a")[0].src = imgA.src;
$("img.b")[0].src = imgB.src;
$('.status').text('Loaded');
}
}
// the part with adding now to the end of the URL here is just for testing purposes to break the cache
// remove that part for deployment
var now = new Date().getTime();
imgA.src = 'http://farm9.staticflickr.com/8545/8675107979_ee12611e6e_o.jpg?' + now;
imgB.src = 'http://farm9.staticflickr.com/8382/8677371836_651f586c99_o.jpg?' + now;
$('.status').text('Loading...');
});
});
该版本的工作演示:http://jsfiddle.net/jfriend00/ChSQ5/
关于javascript - 为什么这个 "loading message"脚本在 FF 中不起作用?(javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16195417/
我正在解析一个 XML 文件,其中包含一些条目,如下所示: FF FF FF FF FF FF FF FF 我将它们保存到 HashMap现在我想转换 String我进入int[] . 但我不知道该怎
我有一个这样的数组: unsigned char array[] = {'\xc0', '\x3f', '\x0e', '\x54', '\xe5', '\x20'}; unsigned char a
输入的正则表达式应该是什么: FF a b FF 其中a和b可以是下面给出的任意组合- FF 1 2 FF FF A C FF FF F D3 FF FF EF 1C FF 我尝试使用 /(FF [a
作为我们的 rebase-heavy 工作流程的一部分,我希望在 master 分支上使用 merge 。特别是,我只想在主题分支重新基于最近的主提交时才 merge ,从而使任何 merge 成为快
在 Qt 项目中,我必须通过以下分隔符拆分 QString 壮举。 壮举。 专长 壮举 特色 特色。 特色 特色 我最好的尝试是 (?: [Ff]eat[.]? )|(?: [[Ff]eaturing
我的网络应用程序在接受上传的图像之前会根据文件扩展名检查前四个字节。一位同事向我展示了他 iPhone 上的图像,但这些图像被拒绝了。它们具有不同的第四个字节(e1 = 225,而不是预期的 e0 =
为什么我在 Firefox Mac 和 Firefox Windows 中有不同的换行行为。如何确保我在两个平台上有相同的换行符? 使用小数位对我来说很重要。 你可以看到我的示例代码和我的截图on J
请检查此屏幕截图! alt text http://img267.imageshack.us/img267/1391/difference.png 这是在 Linux FF(左侧)和 Windows
我有时想做一个--ff-only merge ,制表符完成有点尴尬,因为--ff存在。但是--ff是默认行为,我无法想象想要明确指定它。我可以制作 --ff --ff-only 的同义词?我知道我可以
我编写了一个 jQuery 脚本来检查浏览器高度并与内容面板的高度进行比较。如果面板大于窗口高度,则脚本会使所有内容变小。 它在 Chrome 和 Safari 中运行良好。在 Firefox 上它根
在 IE 中,您可以像以前的渲染引擎一样查看页面。您使用 9 并查看为 8、7、6。 如果这在 Firefox 中可行?我正在使用 FF7,我想看看网站在 3.6 中的显示方式。 这是可能的还是我需要
对我来说,一个典型的 git 工作流程是克隆一个远程存储库并使用 git pull 来保持它是最新的。我不想在 pull 时 merge 提交,所以我使用 --ff-only 选项。 我还为特色工作创
我们可以让 ff-find-other-file 在 ff-search-directories 列出的目录中递归搜索吗? 它不仅会在/usr/include 中搜索,还会在/usr/include/
我遇到了 Java Scripting API together with JavaScript 的问题在某些电脑上。分析转储文件后,我注意到“FF FF”在某些 PC 上被打印为“FD”。下面是代码
这个问题已经有答案了: Why don't flex items shrink past content size? (7 个回答) 已关闭 2 年前。 我们在桌面应用程序(例如 Web 应用程序)中
public class Ex51 { public static void main(String args[]) { Scanner input = new Scanner
这个问题在这里已经有了答案: Why don't flex items shrink past content size? (5 个答案) 关闭 2 年前。
我希望 git merge 默认为 --no-ff 并且 git pull 使用 --ff 当它 merge 获取的分支时。 有没有办法配置 git 自动执行此操作? 最佳答案 我建议设置 merge
假设我有一堆在 Linux 和 Firefox 上运行的 Selenium 测试。现在,我遇到了一些问题,我想查看 FF GUI 来调查这个问题。是否可以连接到服务器,即使用 VNC 查看器查看我的测
我正在使用 jquery 创建命名空间事件。当我使用以下带有 code=112 的函数(函数 bool=false)时,FF 中一切正常,并且 F1 键提交到我的函数,并且该事件不会冒泡以在新选项卡中
我是一名优秀的程序员,十分优秀!