php - 完全加载后显示图像

标签 php jquery html

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

我有两张图片,比如说

$LQimage = "LQabc.jpg";

$HQimg = "HQabc.jpg";

我怎样才能让它发挥作用?

编辑 通过@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>

最佳答案

应该这样做:

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
}

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

<body onload="preLoadImage()">

关于php - 完全加载后显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12295700/

相关文章:

javascript - 为什么我点击 div 元素后会被重定向?

html - 如何在 css 中对重新定位的 div 进行 z-index

php - MySQL基于IF的Join

php - 当我在 Laravel 中运行事务时,具有 InnoDB 的 MySQL 实例是否会锁定行?

php - 将 Doctrine 与领域驱动设计结合使用

javascript - AutoNumeric - 在 Ajax 调用后设置一个值

php - Google Analytics API - 无需使用 "consent screen"进行身份验证即可获取访问 token

Jquery,当哈希值在 url 中时激活脚本

html - 我应该为字典使用什么元标记?

javascript - HTML 元素对象与对象