Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery progressbar on changing image.attr(';src';)_Jquery_Progress Bar_Jquery Attributes_Image Load - Fatal编程技术网

jQuery progressbar on changing image.attr(';src';)

jQuery progressbar on changing image.attr(';src';),jquery,progress-bar,jquery-attributes,image-load,Jquery,Progress Bar,Jquery Attributes,Image Load,我有一个图像在HTML中声明为: <img src="images/image_01.gif" id="man_img_file" width="400" height="300" alt="image_01" /> 现在我想在图像加载时显示一个百分比进度条(即,当新的src图像通过jQuery加载时)。如何做到这一点?可以使用jQuery progressbar完成吗 注意:图像已经在服务器中,我可以在加载之前通过PHP脚本获得图像大小。您可能无法添加进度条,因为我不知道如何通过

我有一个图像在HTML中声明为:

<img src="images/image_01.gif" id="man_img_file" width="400" height="300" alt="image_01" />
现在我想在图像加载时显示一个百分比进度条(即,当新的src图像通过jQuery加载时)。如何做到这一点?可以使用jQuery progressbar完成吗


注意:图像已经在服务器中,我可以在加载之前通过PHP脚本获得图像大小。

您可能无法添加进度条,因为我不知道如何通过使用Javascript确定图像加载了多少

但是,您可以使用
图像
对象,在新图像加载时显示旋转的“加载”gif:

function LoadNewImage(target, url) {
    var newImage = new Image();

    // some overlay div
    loadingOverlay.show();

    newImage.src = url;

    newImage.onload = function() {
        // image is loaded into browser memory, so will display instantly
        target.attr('src', url);

        // hide the overlay again
        loadingOverlay.hide();
    }
}
以下是一些可能有帮助的链接:

谢谢分享

  • 使用jQuery更改“src”属性时,无法动态检查加载的字节量(使用javascript)
  • 但是,在加载图像时,可以显示加载消息/图像。(查看上面Laurence的答案以了解解决方案)

  • 正如Laurence所说,“如果你对HTML5和IE9以上的任何东西感到满意,那么显然你可以用XHR来实现这一点:(参见这里的示例:)”

  • 亲爱的劳伦斯,谢谢。当前,我正在加载新图像时使用加载图像。我可以使用什么技术来确定加载(下载)多少图像?PHP能做到吗?如果你对HTML5和IE9以上的任何东西感到满意,那么你显然可以通过XHR来做到这一点:(参见这里的示例:)我很困惑,为什么你不接受劳伦斯的答案,而不是创建自己的答案来总结他的答案。
    function LoadNewImage(target, url) {
        var newImage = new Image();
    
        // some overlay div
        loadingOverlay.show();
    
        newImage.src = url;
    
        newImage.onload = function() {
            // image is loaded into browser memory, so will display instantly
            target.attr('src', url);
    
            // hide the overlay again
            loadingOverlay.hide();
        }
    }