javascript-jQuery Ajax用图像加载HTML内容,如何检查是否所有图像都已加载?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-jQuery Ajax用图像加载HTML内容,如何检查是否所有图像都已加载?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2200字,纯文字阅读大概需要4分钟。
内容图文
![javascript-jQuery Ajax用图像加载HTML内容,如何检查是否所有图像都已加载?](/upload/InfoBanner/zyjiaocheng/658/930a98e8aea54f41ac71363cbedf735d.jpg)
我正在为我的站点执行无限滚动的脚本:
$(window).scroll(function(){
if($(window).scrollTop() == $(document).height() - $(window).height()){
...
$.ajax({
...
success: function(json) {if(json.success){
...
// here I append an Ajax @response with a new element to my existing HTML
$("#mindIndexSummaryFirst").append(json.items1);
$("#mindIndexSummarySecond").append(json.items2);
$("#mindIndexSummaryThird").append(json.items3);
// here I try to check whether all images are loaded
$("#mindIndexSummary img").on('load', function(){
// and I need to call two functions on loaded element
// hoverImg() need the loaded image to calculate the height
$('.pi-hover-img'+json.course).hoverImg();
$(".popup3").tooltip();
});
}
...
});
加载的HTML元素是这样的:
<div class="page-item pi-hover-img pi-normal">
<a class="overlayPop" href="#">
<a class="item_uri" id="course153" href="#">
<img src="/public/file/course/image/153-course.png" class="pi-item-image">
</a>
<a href="#" class="popup5 hide-text" style="display: none;">
<img src="/public/file/account/image/282-user.jpeg" class="item_image">
</a>
<a class="popup action-button big_interested " id="course_153" href="javascript:;" style="top: -162.5px; left: 83.5px; display: none;"> Mi interessa</a>
<a href="javascript:;">Description</a>
<a class="popup4 hide-text" href="javascript:;">Preview</a>
<span class="popup2" style="display: none;">1€</span>
<a href="/course/153/gestire-un-pubblico">
<h2 style="top: 320px;">Gestire un pubblico</h2>
<span class="rfloat pi-icon">
<img src="/public/img/site_icon/video_course_black.png">
</span>
<div class="clearfix"></div>
</a>
</div>
我加载了5个元素,15张图像,但是有可能加载的元素更少.
问题是我的函数被调用了多次(我用警报对其进行了测试),而且我不确定在图像加载后调用该函数的安全性.
如果有人遇到同样的情况,请给我一些建议(我为英语不好对不起.)
解决方法:
$("#mindIndexSummary img").on('load', function(){ });
将处理程序附加到与查询匹配的每个图像上-因此,每个图像在加载时都会触发该函数-在所有加载完成时不会触发该函数.
您必须跟踪已加载的图像才能说出它们是否都已加载.
遵循以下原则:
var noOfImages = $("#mindIndexSummary img").length;
var noLoaded = 0;
$("#mindIndexSummary img").on('load', function(){
noLoaded++;
if(noOfImages === noLoaded) {
handleAllLoaded();
}
});
内容总结
以上是互联网集市为您收集整理的javascript-jQuery Ajax用图像加载HTML内容,如何检查是否所有图像都已加载?全部内容,希望文章能够帮你解决javascript-jQuery Ajax用图像加载HTML内容,如何检查是否所有图像都已加载?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。