我做了我的网站无限滚动的脚本:jQuery Ajax使用图像加载HTML内容,如何检查是否加载了所有图像?
$(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幅图像,但有一种可能性,即加载的元素更少。
问题是我的函数被多次调用(我用alert来测试),并且我不确定该函数是否在图像加载后调用。
如果有人有同样的情况,请给我一些建议(P.S我为我的坏英语道歉)。
加载事件处理程序不适合你吗? – Halcyon 2013-05-10 15:36:14
加载将触发每个图像加载($(“#mindIndexSummary img”)。长度时间) – Anoop 2013-05-10 15:40:26
@FritsvanCampen是它的工作,现在我解决问题谢谢回答:) – 2013-05-12 20:01:31