当js设置源时,加载检查图像?

时间:2015-02-06 05:04:53

标签: javascript jquery image-loading image-load

当我从jquery设置图像源时,如何检查图像是否已加载。我希望在鼠标上方更改图像,并且由于图像尺寸较大,需要花时间加载,因此需要显示加载图像,直到加载为止。

这是我的代码段:

timer = setInterval(function() {
selector.attr('src',  'localhost/product/image1.jpg');

image1是436x436并且需要花时间加载我想在加载此图片之前显示加载图片

请帮帮我......

5 个答案:

答案 0 :(得分:2)

使用下面的代码

 selector.attr('src',  'localhost/product/image1.jpg');
 //show loader code here 
 var loadImage = new Image();
 loadImage.src = selector.attr('src');
  loadImage.onload = function(){
    // hide loader code here
 }

答案 1 :(得分:0)

//show image on mouseenter event
img.addEventListener('mouseenter',function(){
    this.setAttribute('src',  'localhost/product/image1.jpg');
    //show loading image here
});
img.addEventListener('load',function(){
    //Image loaded. So remove loading image here.
});

答案 2 :(得分:0)

您可以使用以下函数来获取图像的加载回调:

$("#idOfImage").load(function() { /* image loaded */})
               .error(function() { /* error in loading */ });

答案 3 :(得分:0)

这是我在我的网站兄弟所做的事情,它完美地加载了。

<强> HTML

<a href="#" full="http://www.sample.com/wp-content/uploads/2015/01/plot3picture.jpg">
    <img src="http://www.sample.com/wp-content/uploads/2015/01/plot3picture-150x150.jpg" alt="" />                
</a>

<强> JS

$( window ).bind( 'load', function(){
    setTimeout( function(){ 
        $( 'body a' ).each( function(){
            $( this ).find( 'img' ).attr( 'src', $( this ).attr( 'full' ) ); 
        }); 
    }, 300 );
});

答案 4 :(得分:0)

在页面加载时加载两个图像。将其放在标题

试试这个:

if (document.images) {
    img1 = new Image();
    img1.src = "imgpath/image1.png";
    img2 = new Image();
    img2.src = "imgpath/image2.png"";
}
相关问题