gpt4 book ai didi

javascript - 加载图像时显示加载 gif

转载 作者:数据小太阳 更新时间:2023-10-29 03:58:47 24 4
gpt4 key购买 nike

我编写了一些代码,用户可以从 zip 文件上传一些图片。在下一页上,我需要在 85*85 像素的框架中单独显示所有图像。

问题是加载所有图像可能需要一些时间。 所以我想在用户等待图像加载时显示加载中的 gif。

我已经将图像的 src 设置为 正在加载的 gifs,同时我创建了一些带有真实来源作为 id 的复选框

echo "<td><img src=\"beelden/ajax-loader-black-16.png\" id=\"img".$image."\" style=\" width: 85px; height: 85px; border: 1px solid gray; background-color: #fff; padding: 10px;\">";
echo "<input type=\"checkbox\" id=\"img[".$image."]\" name=\"check_image[]\" value=\"".$filename."\" /></td>";
<input type="hidden" name="aantal" id="aantal" value="<?=$image?>" >

然后我创建了一些 javascript 来检查图像是否被加载,当它被加载时,它应该替换图像的源。

<script>
var aantal = document.getElementById("aantal").value;
for(var i = 0; i < aantal; i++){
var source = document.getElementById("img["+i+"]").value;
var img = new Image();
img.onload = function(){
$("#img"+i).attr('src', source);
}();
img.src = source;
}
</script>

但这并不像我预期的那样工作,我认为它会在第一个图像加载后立即触发所有图像。知道我做错了什么或如何解决这个问题吗?

最佳答案

您可以将图像的背景设置为正在加载的 gif。这是一个简单的CSS技巧。然后你就不需要制作 js 脚本了。

.loading {
background: transparent url('http://thinkfuture.com/wp-content/uploads/2013/10/loading_spinner.gif') center no-repeat;
}
<img class="loading" src="http://placehold.it/106&text=1" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=2" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=3" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=4" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=5" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=6" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=7" width="106px" height="106px" />

更新:

如果你有透明图像那么故事会变得有点复杂,但仍然可以用 css 和一些 div 元素来完成。

.image-wrapper {
overflow: hidden;
width: 106px;
height: 106px;
display: inline-block;
}

.image-wrapper img {
float: left;
display: block;
opacity: 0.2; /* simulating a semitransparent image */
}

.image-wrapper:after, .loading {
content: ' ';
background: transparent url('http://thinkfuture.com/wp-content/uploads/2013/10/loading_spinner.gif') center no-repeat ;
background-size : auto 100%;
width: 106px;
height: 106px;
float: left;
display: block;
}
<div class="image-wrapper">
<!-- simulates a hard loading image -->
<img src="http://placehold.it/not-existing" alt="" />
</div>
<div class="image-wrapper">
<img src="http://placehold.it/106x106&text=2" alt="" />
</div>
<div class="image-wrapper">
<img src="http://placehold.it/106x106&text=3" alt="" />
</div>
<div class="image-wrapper">
<img src="http://placehold.it/106x106&text=4" alt="" />
</div>
<div class="image-wrapper">
<img src="http://placehold.it/106x106&text=5" alt="" />
</div>
<div class="image-wrapper">
<img src="http://placehold.it/106x106&text=6" alt="" />
</div>
<div class="image-wrapper">
<img src="http://placehold.it/106x106&text=7" alt="" />
</div>

不幸的是,浏览器在加载时添加了损坏的图标或 ?,这就是图像包含空 alt 的原因;

更新 2:

第二个变体在很大程度上依赖于图像大小,如果你有不同的尺寸而不是加载 gif 将不会被正确推开,作为替代方法是使用第一个变体和一个小的 js 脚本来删除图片加载后立即背景:

$('img').load(function(){
$(this).css('background','none');
});
   .loading {
background: transparent url('http://thinkfuture.com/wp-content/uploads/2013/10/loading_spinner.gif') center no-repeat;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img class="loading" src="http://upload.wikimedia.org/wikipedia/en/2/2d/SRU-Logo-Transparent.png" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=2" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=3" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=4" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=5" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=6" width="106px" height="106px" />
<img class="loading" src="http://placehold.it/106&text=7" width="106px" height="106px" />

关于javascript - 加载图像时显示加载 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29742508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com