如何通过javascript加载图像预览时显示加载gif
问题描述:
在我的页面上,我使用带图像预览的链接。
On my page i am using links with image preview.
当您使用鼠标链接时,它会显示预览图像。
When you go on link with mouse it show a preview image.
以下是演示: http://cssglobe.com/lab/tooltip/03 /
我想在图片预览加载时显示加载gif。
i want to show loading gif while image preiview loading.
加载像这样的gif : http://i.imgur.com/54cQB45.gif
Loading gif like this : http://i.imgur.com/54cQB45.gif
这是我的javascript代码;
here is my javascript code ;
this.screenshotPreview = function(){
/* CONFIG */
xOffset = 10;
yOffset = 30;
// these 2 variable determine popup's distance from the cursor
// you might want to adjust to get the right result
/* END CONFIG */
$("a.screenshot").hover(function(e){
this.t = this.title;
this.title = "";
var c = (this.t != "") ? "<br/>" + this.t : "";
$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");
$("#screenshot")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.fadeIn("fast");
},
function(){
this.title = this.t;
$("#screenshot").remove();
});
$("a.screenshot").mousemove(function(e){
$("#screenshot")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px");
});
};
// starting the script on page load
$(document).ready(function(){
screenshotPreview();
});
和html代码;
<a href="Link" class="screenshot" rel="image link">Link Name</a>
所以我该怎么办?任何想法?
so how can i do ? Any ideas ?
答
您可以使用以下代码段的变体来触发加载后的事件:
You could use a variation of the following code snippet to trigger an event once it's loaded:
var img = $("<img />").attr('src', 'http://somedomain.com/image.jpg')
.load(function() {
if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
alert('broken image!');
} else {
$("#something").append(img);
}
});