滞后的 jquery 预加载器图像

发布于 2024-12-12 07:43:50 字数 1027 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我做我的改变 2024-12-19 07:43:50

您的标题有点令人困惑,所以看来您实际上想要预加载预加载图像:)

我们使用了

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.

鹿港巷口少年归 2024-12-19 07:43:50

我认为发生这种情况是因为当您开始将图像显示为 html 内容时,它会加载然后显示。您已经可以将其加载到您的页面上,例如:

<div id="popup">
    <div id="spinner"> <!-- Assuming that it has display: none in CSS -->
        <center><img src="images/loader.gif" alt="Loading..." /></center>
    </div>

    <!-- Load the content here. -->
</div>

您的脚本看起来像

$(function() {
    $("li#tabs.popup a").click(function(e) {
        $('#popup').find('#spinner')
                   .show()
                   .end()
                   .load('wishlist.php')
                   .slideDown()
                   .addClass('active');

        $('li#tabs.popup a').removeClass('selected');
        $(this).addClass('selected');

        e.stopPropagation();
    });
});

旁注:并且您实际上不必编写$('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:

<div id="popup">
    <div id="spinner"> <!-- Assuming that it has display: none in CSS -->
        <center><img src="images/loader.gif" alt="Loading..." /></center>
    </div>

    <!-- Load the content here. -->
</div>

And your script would look like

$(function() {
    $("li#tabs.popup a").click(function(e) {
        $('#popup').find('#spinner')
                   .show()
                   .end()
                   .load('wishlist.php')
                   .slideDown()
                   .addClass('active');

        $('li#tabs.popup a').removeClass('selected');
        $(this).addClass('selected');

        e.stopPropagation();
    });
});

Sidenote: And you really don't have to write $('li#tabs'), simply $('#tabs') (or more appropriately $('.tabs') would do. When you set the selector to li#tabs it causes Sizzle to filter the result of document.getElementById('tabs') to see if the matching element's tagname is li.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文