如何通过javascript加载图像预览时显示加载gif [英] How to show loading gif while image preview loading via javascript

查看:121
本文介绍了如何通过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);
        }
    });

这篇关于如何通过javascript加载图像预览时显示加载gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆