jquery的页面加载效果

发布于 2024-09-01 23:35:35 字数 292 浏览 3 评论 0 原文

有没有办法使用jquery(或其他方法)在页面加载时显示加载div?

我有一个使用 PHP/MySQL 填充的表,可以包含数千行。然后使用 jquery 的 tablesorter 插件对其进行排序。一切工作正常,但页面有时可能需要 4-5 秒才能完全加载,最好在 div 中显示“正在加载...”消息,该消息在加载整个表格时会自动消失。

我听说过 jquery 的 loadmask 插件 - 这是否适合我的需求,如果没有其他选择?

如果相关的话,加载此表时不会进行任何 AJAX 调用。

提前致谢 安迪

Is there a way to use jquery (or other method) to display a loading div while page loads?

I have a table that is populated using PHP/MySQL and can contain several thousand rows. This is then sorted using the tablesorter plugin for jquery. Everything works fine, however the page can sometimes take 4-5 seconds to fully load and it would be nice to display a 'loading...' message within a div which automatically disappears when whole table is loaded.

I have heard of loadmask plugin for jquery - would this be suitable for my needs and if not any alternative?

No AJAX calls are being made while loading this table if thats relevant.

Thanks in advance
Andy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

意中人 2024-09-08 23:35:35

我认为实现这一点的最简单方法是使用 AJAX。您需要两种方法(脚本)。第一个方法将加载带有加载图像的初始页面和一些 JavaScript 来调用第二个方法。第二种方法将返回表的 HTML 和 javascript(在 body 元素中,而不是 head 中)来调用 tablesorter。 JavaScript 将调用第二个方法,获取 HTML,并将其插入到页面上,替换加载消息。

例子:

 <script type="text/javascript">
     $(function() {
         $.get( '/example.com/secondmethod.php', function(html) {
               $('#loadingImage').replaceWith( html );
         });
     });
 <script>

 <div id="doc">
     <img id="loadingImage" src="/example.com/images/loading.gif" alt="Loading..." />
 </div>

I think the easiest way to accomplish this would be using AJAX. You'd need two methods (scripts). The first would load the initial page with the loading image and a bit of javascript to call the second method. The second method would return the HTML for your table and the javascript (in the body element, not the head) to invoke tablesorter. The javascript would call the second method, get the HTML, and insert it onto the page, replacing the loading message.

Example:

 <script type="text/javascript">
     $(function() {
         $.get( '/example.com/secondmethod.php', function(html) {
               $('#loadingImage').replaceWith( html );
         });
     });
 <script>

 <div id="doc">
     <img id="loadingImage" src="/example.com/images/loading.gif" alt="Loading..." />
 </div>
游魂 2024-09-08 23:35:35

我希望这个链接对你有一些帮助..(因为我相信你不想使用ajax)

http://forums.codecharge.com/posts.php?post_id=85584

谢谢

i hope this link can be of some help to you..(since i believe that you dont want to use ajax)

http://forums.codecharge.com/posts.php?post_id=85584

thanks

幼儿园老大 2024-09-08 23:35:35

不知道这是否是您想要的,但您可以只显示一个“正在加载”以及何时表排序插件准备就绪。显示表格并隐藏加载 div。这种方法有一些缺点和优点。

优点:
1.真正易于实施。
2. 加载div将显示,直到表格排序完成。不知道表排序,但由于它是客户端代码,因此很可能会出现阻塞。

缺点:
1. 如果加载时间长是由于服务器需要很长时间才能响应请求,这对您没有帮助。那么您最好使用 AJAX 解决方案。请参阅 tvanfosson 的回复。

..弗雷德里克

Don't know if this is what you are after but you could just display a that says "loading" and when the tablesort plugin in is ready. Show the table and hide the loading div. This method has some cons and pros.

Pros:
1. Real easy to implement.
2. The loading div will show until the tablesort is done. Don't know about the tablesort but since it's client code it's a big possibility that the hold up is here.

Cons:
1. If the long loading time is due to the it take the server long to respond to the request this will not help you. Then you are better of with an AJAX solution. See tvanfosson's response.

..fredrik

梦在夏天 2024-09-08 23:35:35

我会尝试以下操作:

作为您身体中的第一个子元素,插入 div 元素,例如:

<body>
    <div id="loading">Loading...</div>
    ...

对其进行适当的样式设置,例如:

#loading {
   position: fixed;
   top: 1em;
   left: 1em;
   z-index: 1; /* or a larger number */
   ...
}

然后使用以下 jQuery:

$(document).ready(function() {
    $("#loading")[0].style.visibility = "hidden";
});

我现在无法测试它,我希望它没有太多错误...

I would try the following:

As the first child in your body, insert the div element like:

<body>
    <div id="loading">Loading...</div>
    ...

Style it appropriately like e.g.:

#loading {
   position: fixed;
   top: 1em;
   left: 1em;
   z-index: 1; /* or a larger number */
   ...
}

Then use the following jQuery:

$(document).ready(function() {
    $("#loading")[0].style.visibility = "hidden";
});

I can't test this right now, I hope it doesn't have too many bugs...

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