簡體   English   中英

如何在通過JavaScript加載圖片預覽時顯示加載的gif

[英]How to show loading gif while image preview loading via javascript

在我的頁面上,我正在使用帶有圖像預覽的鏈接。

當您使用鼠標繼續鏈接時,它會顯示預覽圖像。

這是演示: http : //cssglobe.com/lab/tooltip/03/

我想在加載圖片預覽時顯示加載的gif。

像這樣加載gif: http : //i.imgur.com/54cQB45.gif

這是我的JavaScript代碼;

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>

那我該怎么辦? 有任何想法嗎 ?

您可以使用以下代碼段的變體在事件加載后觸發事件:

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);
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM