Sometimes images take some time to render in the browser. I want to show a busy image while the actual image is downloading, and when the image is downloaded, the busy image is removed and the actual image should be shown. How can I do this with JQuery or any javascript?

推荐答案

You can do something like this:

// show loading image
$('#loader_img').show();

// main image loaded ?
$('#main_img').on('load', function(){
  // hide/remove the loading image
  $('#loader_img').hide();
});

You assign load event to the image which fires when image has finished loading. Before that, you can show your loader image.

Jquery相关问答推荐

为什么我不能在 window.onload 事件的处理程序中将 $ jQuery 对象作为参数传递?

Jquery $(this) 子 Select 器

用 JS 触发 CSS 悬停

缺少 .map 资源?

jQuery 绑定点击 *ANYTHING* 但 *ELEMENT*

使用 jQuery Validate 确认密码

调用 e.preventDefault() 后提交表单

jQuery计算所有文本字段中的值的总和

使用 D3.js(IE、safari 和 chrome)创建 SVG 后,如何保存/导出 SVG 文件?

blueimp 文件上传插件中的 maxFileSize 和 acceptFileTypes 不起作用.为什么?

jQuery:value.attr 不是函数

如何检测 window.print() 完成

jQuery:如何从 $.ajax.error 方法中获取 HTTP 状态代码?

如何使用 Twitter Bootstrap 自动关闭alert

测试两个元素是否相同

使用 JavaScript 获取用户代理

Backbone.js - 事件,知道点击了什么

jQuery:如何找到父母的特定子元素?

jQuery clone() 不克隆事件绑定,即使使用 on()

使用 jQuery Select 空文本输入