如何让 AJAX 触发浏览器的加载指示器
我正在制作一个支持 ajax 的实验室调度程序,并且某些 ajax 操作速度并不快。
在 Gmail 中,当您进入收件箱、发送消息等时,浏览器的行为就像正在加载(在 FF 中,停止按钮变为启用状态,进度条出现),但它不是在新页面上,而是通过 AJAX 完成的。
他们是如何做到这一点的?我有一个旋转的小指示器,但如果让浏览器表现得像正在加载一样,那就太好了。有什么想法吗?
I'm making an ajax-enabled lab scheduling program, and some of the ajax operations aren't exactly quick.
In Gmail, when you go to your inbox, send a message, etc. the browser acts like it's loading (In FF the stop button becomes enabled, the progress bar appears), but it's not on a new page, it's done via AJAX.
How do they do this? I have a little spinny indicator, but it would be a nice touch to have the browser act like it's loading. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为这是您的答案。 (Reverse-AJAX 或来自 Amazing.com 的“慢加载”技术)
看起来像 GMail 和 Facebook 方法(浏览器将页面显示为“正在加载”,并带有加载图标等 - 它只是模拟,因为有后台 ajax 请求) :)
I think this is your answer. (Reverse-AJAX or "Slow Load" technique from Obviously.com)
Looks like the GMail and Facebook method (browser is showing page as "loading" with loading icons etc. - it is just simulating, because there is a background ajax request) :)
我在这里找到了相关的答案:浏览器在ajax请求完成后继续“加载”。
您可以创建一个 iframe 并打开和关闭其 contentDocument,这似乎可以启动和停止浏览器的本机加载动画。请参阅此处的示例:http://jsfiddle.net/KSXkS/1/。
I found a related answer here: Browser continues 'loading' after ajax request finishes.
You can create an iframe and open and close its contentDocument which seems to start and stop the browser's native loading animation. See an example here: http://jsfiddle.net/KSXkS/1/.
许多用户甚至不理解或注意到浏览器的本机加载指示器。如果它确实是一个长时间运行的负载,那么最好的用户体验是将其指示为 Web 应用程序的一部分。请记住,上下文为王,这是向用户传达正在发生的情况以及哪些功能仍然可用的机会。
如果您的整个用户界面在请求时变得无效,则适合使用带有加载指示器的覆盖层。稍微不透明的覆盖层可以与用户进行交流,而不会造成视觉中断。
如果您只是需要显示请求已启动并且正在运行,那么启动请求的按钮旁边的微调器是最好的。成功后,旋转器可以替换为绿色复选标记或其他常见指示符,这些指示符会在短时间内淡出。
这些是在许多谷歌应用程序中发现的常见模式。
Many users don't even understand or notice the browser's native loading indicators. If it is really a long running load the best user experience would be to indicate it as part of your web application. Remember context is king and this is an opportunity to communicate to your user what is going on and what functionality is still available.
If your entire user interface becomes invalid at the time of the request an overlay with a loading indicator is appropriate. A slightly opaque overlay communicates to the user without a disruptive break in vision.
If you simply need to show the request has started and is working, a spinner next to the button that started the request is best. On success, the spinner can be replaced with a green check mark or other common indicator that fades out after a short period.
These are common patterns found in many of google's applications.
我相信你想要这样的东西。
我添加了一个 API 来提取 github.com 的地理位置(不相关,但我认为它用于示例 API 的目的)。
这是简单的
JavaScript
和JQuery
代码,用于在发出AJAX
请求时显示/隐藏加载指示器。I believe you want something like this.
I have added an API to pull geo location of
github.com
(not related, but I think it serves the purpose of sample API).It's simple
JavaScript
andJQuery
code to show/hide the loading indicator while making theAJAX
request.可能的解决方案是使用 iframe 来触发加载。但根据之前的答案,iframe 方法无法在 chrome 上工作。
您的需要是在浏览器上显示加载图标,所以我认为另一种方法是创建
假加载图标
。您可以手动更新浏览器图标。有一些关于如何更新浏览器图标的教程。
然后,尝试使图标具有动画效果。大多数浏览器不支持
gif
图标,因此您需要做的是每隔几秒更新一次图标。这是演示代码:
我创建了一个快速演示。 演示
通过这个演示,您可以发现图标动画运行不流畅。这是因为每次我们更新图标时,浏览器都会再次请求图标。
因此将图像转换为base64格式比它工作顺利。
Base64 demo
现在你只需要找到加载图标与 < code>Chrome 等流行浏览器,然后在调用 ajax 时触发
假加载
。这里我粘贴一些更改图标教程:
The probable solution is to use
iframe
to trigger the loading. But based on previous answer, iframe method cannot work on chrome.Your need is to show the loading icon on browser, so I think another way is to create
fake loading icon
.You can update the browser icon manually. There are some tutorials about how to update browser icon.
And then, try to make icon animate. Most of browser doesn't support
gif
icon, so what you need to do is update icon in every few seconds.This is demo code:
I create a quick demo. Demo
With this demo, you can find that the icon animation doesn't run smoothly. That is because everytime we update the icon, browser request the icon again.
So transform the image to base64 format than it works smoothly.
Base64 demo
Now you just need to find the loading icon is the same with
Chrome
and other popular browsers, then trigger thefake loading
when you call ajax.Here I paste some change icon tutorial: