我在这个网站上有 ajax 导航,并且有一个预加载器黑点显示 4 秒然后消失。有没有更好的方法让它等待至少第一个图像或全部加载?
这是我的网站:http://www.joe-tsao.com/
这是预加载器的代码
var id = jQuery(this).parent().attr('data-id');
var ajaxurl = admin_ajax.admin_url + 'admin-ajax.php';
var data = {
'action': 'hb_load_page',
'id': id
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function(html) {
jQuery('.page-details').html('<div class="row">' + html + '</div>');
setTimeout(function() {
jQuery('.hb-loader').hide();
}, 4000);
});
最佳答案
您可以使用.done
方法,如官方文档所示:
jQuery.post(ajaxurl, data, function(html) {
jQuery('.page-details').html('<div class="row">' + html + '</div>');
}).done(function(data){
jQuery('.hb-loader').hide();
});
https://api.jquery.com/jquery.post/#jQuery-post-url-data-success-dataType
关于javascript - Jquery ajax 导航,等待图像加载的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41383193/