我在网页上有一个图像,该图像是由服务器端CGI程序动态生成的。该图像由计时器定期刷新和/或根据用户输入进行更改。所以我有一个像
// <img id="screen" src=""> is elsewhere in the page
function reloadImage() {
$("#screen").attr("src", make_cgi_url_based_on_form_inputs());
}
这工作得很好,但有时需要一段时间来加载图像。因此,我希望出现某种消息,提示“正在加载图像...”,但是当图像已加载并在浏览器中显示时,该图像便消失了。
有没有可以执行此操作的Javascript事件?另外,还有其他方法可以通过Ajax或其他方式加载/更改/更新图像并检测加载完成的时间吗?
最佳答案
您可以尝试以下jquery解决方案:http://jqueryfordesigners.com/image-loading/
$(function () {
var img = new Image();
$(img).load(function () {
//$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
$(this).hide();
$('#loader').removeClass('loading').append(this);
$(this).fadeIn();
}).error(function () {
// notify the user that the image could not be loaded
}).attr('src', 'http://farm3.static.flickr.com/2405/2238919394_4c9b5aa921_o.jpg');
});