javascript - 如何防止浏览器的 img 绘画可见?
问题描述
当我向 DOM 添加新图像时,有时我可以看到图像是从上到下绘制的。是否有一种简单的 javascript 技术或事件处理程序可用于仅在浏览器完成绘制/渲染后才显示完整图像?
解决方案
You could use JavaScript to hide the image until it is fully loaded.
HTML:
<img id="image" src="...">
JavaScript:
var image = document.querySelector('#image');
image.style.opacity = '0'; //set opacity to 0 (fully transparent)
image.onload = function(){ //when image is loaded, execute function
image.style.opacity = '1'; //set opacity to 1 (fully visible)
}
Because I use opacity property in this example, you could even animate the image appearing.
CSS:
#image {
transition: opacity .5s;
}
推荐阅读
- python - GraphQL 和 Python - `No query string was present` 错误
- c++ - 使用 Mxnet C++ API 输出值
- arduino - Arduino:if else 语句,如果所有数字都在范围内,如果至少一个不在范围内?
- apache-zeppelin - 如何从同一本地计算机上的另一个笔记本运行 Apache Zeppelin 笔记本?
- assembly - MIPS 程序不工作错误:在表中找不到符号出口
- javascript - 如何防止表单在 Jquery 提交事件中被重新提交?
- php - Shopify 使用 laravel 自定义店面
- r - 如何根据 2 个标准生成第 6 个最差值并将结果插入单独的列?
- visual-studio-code - 在 VS Code 中打开项目时启动应用程序?
- laravel - 如何使用 Laravel/Valet/Xdebug 测试 PhpStorm 的 Web 服务器调试验证?