如何在显示网页之前预加载网页?
我创建了一个包含多个图像的简单网页,但是当用户访问它时,浏览器一次加载一个图像,而不是一次加载所有图像。
我想首先在页面中央显示一个“正在加载”gif,然后在下载所有图像后,立即向用户显示整个网页。
我该怎么做?
I have created a simple webpage with several images, but when a user visits it, the browser loads the images one at a time, instead of all at once.
I want instead to first show a "loading" gif in the center of the page and then, when all the images are downloaded, show the entire webpage to the user at once..
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过将加载程序图像放在
标记中的某个位置来显示加载程序图像,并在显示所有图像时使用下面的 js 代码隐藏它:
其中
elementID
应该是作为加载器元素/标签的 id。当所有图像/帧/外部资源加载时,
load
事件将触发,因此,当该事件触发时,所有图像都已加载,因此我们在此处隐藏加载消息/图像。You can show a loader image by putting it somewhere im
<img>
tag and use below js code to hide it later on when all images are shown:Where
elementID
is supposed to be the id of loader element/tag.The
load
event fires when all images/frames/external resources are loaded, so by the time that event fires, all images are loaded and we therefore hide the loading message/image here.编辑:我遵循Keltex的回答。 这是一个更好的解决方案。我将把我的内容留在这里供后代使用(除非我应该完全删除内容和我的答案?我是新来的)。
过去经常使用的另一个解决方案是创建一个预加载所有图像的登陆页面。预加载完成后,它会重定向到实际站点。为了使其工作,您需要获取要加载的所有图像的 URL,然后执行如下操作:
Edit: I defer to Keltex's answer. It's a much better solution. I'll leave mine here for posterity (unless I should delete the content and my answer entirely? I'm new here).
Another solution, which was used fairly frequently in the past, is to create a landing page that preloads all of your images. When the preloading is done, it redirects to the actual site. In order for this to work, you'd need to get the URLs to all of the images you want to load, and then do something like this:
您可以使用 JQuery 来完成此操作。假设您的页面如下所示:
您可以使用 JQuery 函数在加载整个页面时显示页面内容:
You can do this with JQuery. Say your page looks like this:
You can have a JQuery function to show pagecontent when the entire page is loaded:
HTML
JavaScript
CSS
HTML
JAVASCRIPT
CSS
创建一个带有类名 preloader 的 div 并将加载器放入该 div 中。
像下面一样设置类预加载器的样式
Html
Jquery
一个简单的页面加载器已准备就绪......
Create a div with class name preloader and put a loader inside that div.
style the class preloader just like below
Html
Jquery
A simple page loader is ready....