滞后的 jquery 预加载器图像
我正在使用 jquery 在加载主要内容之前加载预加载图像。问题是,当我点击链接时,加载图像至少需要 5 秒才能显示。我希望在内容显示之前单击链接立即显示。
这是jquery;
$(function() {
$("li#tabs.popup a").click(function(e) {
$("#popup").slideDown().addClass('active');
$('#popup').html('<center><img src="images/loader.gif" /></center>');
$('#popup').load('wishlist.php');
$('li#tabs.popup a').removeClass('selected');
$(this).addClass('selected');
e.stopPropagation();
});
});
经过一番烦躁之后,我找到了问题的答案。由于某种原因,将加载程序放在所有其他功能之上将导致它顺利加载并立即单击。
所以我现在用的不是上面的代码,
$(function() {
$("li#tabs.popup a").click(function(e) {
$('#popup').html('<center><img src="images/loader.gif" /></center>');
$("#popup").slideDown().addClass('active');
$('#popup').load('wishlist.php');
$('li#tabs.popup a').removeClass('selected');
$(this).addClass('selected');
e.stopPropagation();
});
});
它运行得非常完美!
Am using jquery to load a preloading image before the main content is loaded. The problem is that, when I click on the link, the loading image takes atleast 5secs before it shows. I would like it to show immediately I click on the link before the content shows.
Here's the jquery;
$(function() {
$("li#tabs.popup a").click(function(e) {
$("#popup").slideDown().addClass('active');
$('#popup').html('<center><img src="images/loader.gif" /></center>');
$('#popup').load('wishlist.php');
$('li#tabs.popup a').removeClass('selected');
$(this).addClass('selected');
e.stopPropagation();
});
});
After much fidgeting I found the answer to the question. For some reason, placing the loader above all other functions will cause it to load smoothly and immediately onclick.
So instead of the code above, I did
$(function() {
$("li#tabs.popup a").click(function(e) {
$('#popup').html('<center><img src="images/loader.gif" /></center>');
$("#popup").slideDown().addClass('active');
$('#popup').load('wishlist.php');
$('li#tabs.popup a').removeClass('selected');
$(this).addClass('selected');
e.stopPropagation();
});
});
now it works just perfect!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的标题有点令人困惑,所以看来您实际上想要预加载预加载图像:)
我们使用了
You're title is a little confusing, so it seems you actually want to preload your preloading image :)
We've used this script in the past to ensure that all image references in our css are preloaded. I would then change your "preloading" animation to use css (you can set the background image to your animated gif) rather than creating an image tag on the fly.
我认为发生这种情况是因为当您开始将图像显示为 html 内容时,它会加载然后显示。您已经可以将其加载到您的页面上,例如:
您的脚本看起来像
旁注:并且您实际上不必编写
$('li#tabs')
,只需$('#tabs')
(或更恰当地说$('.tabs')
即可。当您将选择器设置为li#tabs
它导致 Sizzle 过滤结果document.getElementById('tabs')
查看匹配元素的标记名是否为li
。I suppose that this happens because when you start displaying the image as html content, it loads and then shows up. You could already load it onto your page, for instance:
And your script would look like
Sidenote: And you really don't have to write
$('li#tabs')
, simply$('#tabs')
(or more appropriately$('.tabs')
would do. When you set the selector toli#tabs
it causes Sizzle to filter the result ofdocument.getElementById('tabs')
to see if the matching element's tagname isli
.