如何创建“请稍候,正在加载...”使用 jQuery 制作动画?
我想在我的网站上放置一个“请稍候,加载”旋转圆圈动画。我应该如何使用 jQuery 来完成这个任务?
I would like to place a "please wait, loading" spinning circle animation on my site. How should I accomplish this using jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
您可以通过各种不同的方式来做到这一点。它可能是微妙的,如页面上显示“正在加载...”的小状态,也可能是大声的,如在加载新数据时整个元素使页面变灰。我在下面采用的方法将向您展示如何完成这两种方法。
设置
首先让我们从 http://ajaxload.info 获得一个漂亮的“加载”动画
我将使用
让我们创建一个可以在制作 ajax 时随时显示/隐藏的元素请求:
CSS
接下来让我们给它一些天赋:
最后是 jQuery
好吧,我们来看看 jQuery。下一部分实际上非常简单:
就是这样!每当
ajaxStart
或ajaxStop
事件被触发时,我们都会将一些事件附加到 body 元素。当 ajax 事件开始时,我们将“loading”类添加到正文中。当事件完成时,我们从主体中删除“正在加载”类。查看实际操作:http://jsfiddle.net/VpDUG/4952/
You could do this various different ways. It could be a subtle as a small status on the page saying "Loading...", or as loud as an entire element graying out the page while the new data is loading. The approach I'm taking below will show you how to accomplish both methods.
The Setup
Let's start by getting us a nice "loading" animation from http://ajaxload.info
I'll be using
Let's create an element that we can show/hide anytime we're making an ajax request:
The CSS
Next let's give it some flair:
And finally, the jQuery
Alright, on to the jQuery. This next part is actually really simple:
That's it! We're attaching some events to the body element anytime the
ajaxStart
orajaxStop
events are fired. When an ajax event starts, we add the "loading" class to the body. and when events are done, we remove the "loading" class from the body.See it in action: http://jsfiddle.net/VpDUG/4952/
至于实际加载图像,查看此网站以获取一系列选项。
至于在请求开始时显示带有此图像的 DIV,您有几种选择:
A) 手动显示和隐藏图像:
B) 使用 ajaxStart 和 ajaxComplete:
使用此该元素将针对任何请求显示/隐藏。可能好也可能坏,取决于需要。
C) 对特定请求使用单独的回调:
As far as the actual loading image, check out this site for a bunch of options.
As far as displaying a DIV with this image when a request begins, you have a few choices:
A) Manually show and hide the image:
B) Use ajaxStart and ajaxComplete:
Using this the element will show/hide for any request. Could be good or bad, depending on the need.
C) Use individual callbacks for a particular request:
除了 Jonathan 和 Samir 的建议(顺便说一句,这两个答案都很棒!),jQuery 有一些内置事件,当您发出 ajax 请求时,它会为您触发这些事件。
有
ajaxStart
事件...它的兄弟,
ajaxStop
事件它们共同构成了一种在页面上任何地方发生任何 ajax 活动时显示进度消息的好方法。
HTML:
脚本:
Along with what Jonathan and Samir suggested (both excellent answers btw!), jQuery has some built in events that it'll fire for you when making an ajax request.
There's the
ajaxStart
event...and it's brother, the
ajaxStop
eventTogether, they make a fine way to show a progress message when any ajax activity is happening anywhere on the page.
HTML:
Script:
您可以从 Ajaxload 获取旋转圆圈的动画 GIF - 将其粘贴在网站文件层次结构中的某个位置。然后,您只需添加具有正确代码的 HTML 元素,并在完成后将其删除。这相当简单:
然后您只需要在 AJAX 调用中使用这些方法:
这有一些注意事项:首先,如果您有两个或更多位置可以显示加载图像,则您将需要保留以某种方式跟踪同时运行的调用数量,并且仅在全部完成后隐藏。这可以使用一个简单的计数器来完成,它应该适用于几乎所有情况。
其次,这只会在成功的 AJAX 调用时隐藏加载图像。要处理错误状态,您需要查看
$.ajax
,它比$.load
、$.get
等更复杂,但也更灵活。You can grab an animated GIF of a spinning circle from Ajaxload - stick that somewhere in your website file heirarchy. Then you just need to add an HTML element with the correct code, and remove it when you're done. This is fairly simple:
You then just need to use these methods in your AJAX call:
This has a few caveats: first of all, if you have two or more places the loading image can be shown, you're going to need to kep track of how many calls are running at once somehow, and only hide when they're all done. This can be done using a simple counter, which should work for almost all cases.
Secondly, this will only hide the loading image on a successful AJAX call. To handle the error states, you'll need to look into
$.ajax
, which is more complex than$.load
,$.get
and the like, but a lot more flexible too.Jonathon 的优秀解决方案在 IE8 中崩溃(动画根本不显示)。要解决此问题,请将 CSS 更改为:
Jonathon's excellent solution breaks in IE8 (the animation does not show at all). To fix this, change the CSS to:
SVG 动画可能是解决这个问题的更好方法。您无需担心编写 CSS,与 GIF 相比,您将获得更好的分辨率和 Alpha 透明度。您可以使用一些非常好的 SVG 加载动画:http://samherbert.net/svg-loaders/< /a>
您还可以通过我构建的服务直接使用这些动画: https://svgbox.net/iconset/loaders 。它允许您自定义填充并允许直接使用(盗链)。
为了完成您想要使用 jQuery 执行的操作,您可能应该隐藏一个加载信息元素,并在想要显示加载器时使用
.show()
。例如,此代码在一秒钟后显示加载程序:SVG animations are probably a better solution to this problem. You won't need to worry about writing CSS and compared to GIFs, you'll get better resolution and alpha transparency. Some very good SVG loading animations that you can use are here: http://samherbert.net/svg-loaders/
You can also use those animations directly through a service I built: https://svgbox.net/iconset/loaders. It allows you to customize the fill and direct usage (hotlinking) is permitted.
To accomplish what you want to do with jQuery, you probably should have a loading info element hidden and use
.show()
when you want to show the loader. For eg, this code shows the loader after one second:jQuery 为 AJAX 请求开始和结束时提供事件挂钩。您可以连接这些来显示您的加载程序。
例如,创建以下 div:
在样式表中将其设置为
display: none
。您可以按照自己想要的方式设计它。如果您愿意,您可以在 Ajaxload.info 生成漂亮的加载图像。然后,您可以使用类似下面的内容,使其在发送 Ajax 请求时自动显示:
只需将此 Javascript 块添加到页面末尾在关闭正文标记之前或任何您认为合适的位置即可。
现在,每当您发送 Ajax 请求时,都会显示
#spinner
div。当请求完成后,它将再次隐藏。jQuery provides event hooks for when AJAX requests start and end. You can hook into these to show your loader.
For example, create the following div:
Set it to
display: none
in your stylesheets. You can style it whatever way you want to. You can generate a nice loading image at Ajaxload.info, if you want to.Then, you can use something like the following to make it be shown automatically when sending Ajax requests:
Simply add this Javascript block to the end of your page before closing your body tag or wherever you see fit.
Now, whenever you send Ajax requests, the
#spinner
div will be shown. When the request is complete, it'll be hidden again.如果您使用 Turbolinks With Rails,这是我的解决方案:
这是 CoffeeScript
这是基于 Jonathan Sampson 的第一个出色答案的 SASS CSS
If you are using Turbolinks With Rails this is my solution:
This is the CoffeeScript
This is the SASS CSS based on the first excellent answer from Jonathan Sampson
出于对其他帖子的充分尊重,您在这里有一个非常简单的解决方案,使用 CSS3 和 jQuery,无需使用任何其他外部资源或文件。
With all due respect to other posts, you have here a very simple solution, using CSS3 and jQuery, without using any further external resources nor files.
就像 Mark H 所说的那样,blockUI 就是这样。
例如:
观察:我在 http://www.ajaxload.info/
Like Mark H said the blockUI is the way.
Ex.:
Obs.: I got the ajax-loader.gif on http://www.ajaxload.info/
这将使按钮消失,然后“正在加载”的动画将出现在它们的位置,最后只显示成功消息。
This would make the buttons disappear, then an animation of "loading" would appear in their place and finally just display a success message.
我见过的大多数解决方案要么期望我们设计一个加载覆盖层,将其隐藏,然后在需要时取消隐藏,要么显示 gif 或图像等。
我想开发一个强大的插件,只需一个简单的 jQuery 调用即可可以显示加载屏幕并在任务完成时将其拆除。
下面是代码。这取决于 Font Awesome 和 jQuery:
只需将上述内容放入 js 文件中并将其包含在整个项目中即可。
CSS 添加:
调用:
Most of the solutions I have seen either expects us to design a loading overlay, keep it hidden and then unhide it when required, or, show a gif or image etc.
I wanted to develop a robust plugin, where with a simply jQuery call I can display the loading screen and tear it down when the task is completed.
Below is the code. It depends on Font awesome and jQuery:
Just put the above in a js file and include it throughout the project.
CSS addition:
Invocation:
请注意,使用 ASP.Net MVC 时,使用
using (Ajax.BeginForm(...
) 时,设置ajaxStart
将不起作用。请使用
AjaxOptions
解决这个问题:Note that when using ASP.Net MVC, with
using (Ajax.BeginForm(...
, setting theajaxStart
will not work.Use the
AjaxOptions
to overcome this issue:根据 https://www.w3schools.com/howto/howto_css_loader.asp,此是一个没有 JS 的 2 步过程:
1. 在您想要旋转器的位置添加此 HTML:
2. 添加此 CSS 以使实际的旋转器:
Per https://www.w3schools.com/howto/howto_css_loader.asp, this is a 2-step process with no JS:
1.Add this HTML where you want the spinner:
<div class="loader"></div>
2.Add this CSS to make the actual spinner:
我使用 CSS3 制作动画
I use CSS3 for animation
Fontawesome 有一个加载动画图标,如果您已经在使用 font Awesome,可以直接在您的项目中使用,无需任何额外的数据负担。
然后是jquery,您可以使用以下代码来显示隐藏元素。
Fontawesome has a loading animation icon which can be used directly in your project without any extra data burden if you are already using font awesome.
Then is jquery you can use following code to show hide the element.
将此代码放在您的 body 标签上
并使用此 jquery 脚本
请参阅此处的完整示例。
http://bootdey.com/snippets/view/page-loader
Place this code on your body tag
And use this jquery script
See a full example working here.
http://bootdey.com/snippets/view/page-loader
我还发现了这样一个问题(挑战),即在数据库响应期间通知用户一些“信息”。
我的解决方案可能与这里介绍的有点不同,是好是坏?我不知道,这对我来说已经足够了。
I have also found such a problem (challenge) to inform the user about some "info" during the DB response.
My solution is probably a little different than presented here, is it good or bad? I do not know, it was enough for me.
HTML
jQuery
HTML
jQuery
这很简单。
HTML
CSS
JS - JQuery
It is very simple.
HTML
CSS
JS - JQuery