完全加载后显示图像

pra*_*tri 5 html php jquery

我想在浏览器中完全加载或下载图像后加载图像。我不想在下载图像时显示图像。相反,我想显示该图像的较低质量版本,直到在背景中加载高质量图像,并且当图像在背景中完全加载时,我想用该图像替换较低质量的图像。

我有两张图片,说

$LQimage = "LQabc.jpg";

$HQimg = "HQabc.jpg";
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它工作?

编辑 通过@codebox 发布的答案,此代码有效..谢谢:)

<html>
<head>
<script type="text/javascript">
function load(){
    var img = new Image();
    var imgUrl = 'HQabc.jpg';
    img.onload = function(){
        // this gets run once the image has finished downloading
        document.getElementById('pp').src = imgUrl;
    }
    img.src = imgUrl; // this causes the image to get downloaded in the background
}
</script>
</head>
<body onload="load()">
    <img id="pp" src="LQabc.jpg" width=600/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

cod*_*box 5

这应该这样做:

function preLoadImage(){
    var img = new Image();
    var imgUrl = 'HQabc.jpg';
    img.onload = function(){
        // this gets run once the image has finished downloading
        document.getElementById('idOfImgElement').src = imgUrl;
    }
    img.src = imgUrl; // this causes the image to get downloaded in the background
}
Run Code Online (Sandbox Code Playgroud)

如果在页面加载后运行此函数,它应该可以工作:

<body onload="preLoadImage()">
Run Code Online (Sandbox Code Playgroud)