简体   繁体   English

在Firefox中等待图像加载时如何制作图像

[英]How to make a loading image while waiting for image to load in firefox

I'm having this issue with firefox, works perfectly on chrome. 我在Firefox中遇到了这个问题,可以在chrome上完美运行。

I'm creating a slide gallery, and the user can click next or previous to switch between the images in the gallery. 我正在创建幻灯片库,用户可以单击下一个或上一个在库中的图像之间切换。 I'm trying to create a loading image in-between switching images while the user is waiting for the actually image to loading. 我正在尝试在用户等待实际图像加载时在切换图像之间创建加载图像。 The code I have currently to do this as follows: 我目前要执行的代码如下:

    function changePicture()
    {   
            var imagevar = document.getElementById('theimage');

            //display loading image
            imagevar.src = "/img/LoadingPage.png";

            //Load actual Image
            var imgURL = <IMAGE PATH>
            imagevar.onload = ShowImage(imgURL);    
    }

    function ShowImage(imgURL)
    {
        var imagevar = document.getElementById('theimage');
        imagevar.src = imgURL;
    }

So the logic in there is pretty simple. 因此,其中的逻辑非常简单。 Load the "Loading Image" when switching between images, when the "Loading Image" has finished loading then start loading in the "Actual Image". 在图像之间切换时,加载“加载图像”,当“加载图像”完成加载后,再开始在“实际图像”中加载。 When the actual image is completed loading then it should be displayed. 实际图像加载完成后,应显示它。

In firefox all that happens is that the image will hang on the previous image while the next image is loading and when the next image is loaded it will display the new image. 在firefox中,所有发生的情况是,在加载下一个图像时,该图像将挂在上一个图像上,而在加载下一个图像时,它将显示新图像。 But the "Loading Image" never gets displayed/shown. 但是“加载图像”永远不会显示/显示。

I've also tried to just out right hide the image until it is loaded and on the onLoad unhide it again. 我还尝试过直接隐藏图像,直到图像被加载,然后在onLoad上再次取消隐藏它。 But all that happens is the Image will just hang on the previous image until the current image is loading, never actually hiding the image. 但是所有发生的事情是Image会一直挂在前一个图像上,直到加载当前图像为止,而从未真正隐藏该图像。

And all of these techniques work perfectly fine on chrome. 所有这些技术在chrome上都可以正常工作。 Can someone help me out on how to get this to work on firefox or knows of a better way? 有人可以帮我解决如何在Firefox上工作或知道更好的方法吗?

The problem is that your logic is operating on the same image element. 问题是您的逻辑在同一图像元素上运行。 Loading images are supposed to be separate from the image that's used to display stuff. 加载图像应该与用于显示内容的图像分开。

Here's code that loads a new image on the image element, and while that happens,we display the loading image somewhere until the other loads. 这是将新图像加载到图像元素上的代码,在这种情况下,我们将加载图像显示在某个位置,直到其他图像加载为止。

function showGalleryImage(path){
  //get our placeholder
  var theImage = document.getElementById('theimage');

  //show our loading image. usually overlaying theImage
  showLoadingImage(true);

  //set our handler to check if loaded
  theImage.onload = function(){
    //when loaded, hide
    showLoadingImage(false);
  }

  //start loading the new image
  theImage.src = path;
}

function showLoadingImage(state){
  //get our loading image placeholder
  var loadingImage = document.getElementById('loadingImage');

  //set our display states
  var displayState = state ? 'block' : 'none';
  loadingImage.style.display = displayState;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM