如何在没有 JavaScript 的情况下预加载图像?
在我的 HTML 页面之一上,当我将鼠标悬停在某些链接上时,会显示一些大图像,并且加载这些图像需要一些时间。
我不想使用 JavaScript 来预加载图像。 有什么好的解决办法吗?
On one of my HTML pages there are some large images that are shown when I mouse hover over some links and it takes time to load those images.
I don't want to use JavaScript to preload images. Are there any good solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
HTML5 有一种新方法可以做到这一点,即
链接预取
。只需在 HTML 中添加所需数量的
link
标记即可。 当然,较旧的浏览器不会以这种方式加载内容。注意
上面的代码不适用于 Apple Safari safari 直到版本 12 才支持预取 https://caniuse.com/# search=prefetch
UPDATE
如果您的服务器使用 HTTP2 提供服务,您还可以在响应中添加一个
Link
标头,使用 HTTP2 服务器推送。HTML5 has a new way to do this, by
link prefetching
.Just add as many
link
tags as you need in your HTML and you are good to go. Of course, older browsers will not load the content this way.Note
Above code will not work for Apple Safari safari does not support prefetch til now version 12 https://caniuse.com/#search=prefetch
UPDATE
If your server is served with HTTP2, you can also add a
Link
header in your response a made use of HTTP2 Server Push.来自 http://snipplr.com/view/2122/css-image-preloader
From http://snipplr.com/view/2122/css-image-preloader
无需预加载图像。 我不明白为什么 99% 的人认为悬停效果必须使用 2 张图像。 没有这个必要,而且使用 2 个图像看起来很糟糕。
我知道的唯一好的解决方案是对 A 元素使用 CSS(或对所有其他按钮使用简单的 JS)。 当按钮悬停时,将背景位置设置为某个偏移量。
就这样,您可以在下面看到使用的图像:
(来源:margonem.pl)
编辑:您可以也可以以其他方式使用它。 您可以隐藏图像,而不是切换图像。 所以起点是“背景位置:0 -100px”,悬停时为“0 0”。
这种技术称为 CSS sprites - 这里有对其的详细描述: http://css-tricks.com /css-精灵/
There is no need to preload images. I can't understand why 99% people thinks, that hover effects have to use 2 images. There is no such need, and using 2 images makes it look bad.
The only good solution I know is to use CSS for A elements (or easy JS for all other buttons). When button us hovered set background-position to some offset.
That's all, image used you can see below:
(source: margonem.pl)
Edit: You can also use it other way. Instead of toggling image, you can hide your image. So starting point would be "background-position:0 -100px" and on hover "0 0".
This technique is called CSS sprites - here is good description of it: http://css-tricks.com/css-sprites/
我在这里还没有看到提到的一项技术,如果您不需要担心 IE6 和 IE6,那么该技术非常有用。 7、使用
:after
伪选择器将图像作为content
添加到页面上的元素。 下面的代码摘自本文:唯一的缺点是可以看出,与使用 JavaScript 预加载图像相比,没有简单的方法将它们从内存中释放。
A technique I didn't see mentioned here yet, which works great if you don't need to worry about IE6 & 7, is to use the
:after
pseudo-selector to add your images ascontent
to an element on the page. Code below lifted from this article:The only downside I can see to this compared to using JavaScript to preload the images is that there is no easy way to release them from memory.
您可以使用隐藏的 div 来放入图像。就像这样,
但这仅适用于图像,即。 如果您尝试对 .swf 文件执行相同操作,则会出现问题。 如果 .swf 文件不可见,Firefox 不会运行该文件。
You could use a hidden div to put images in. Like so
This only works for images though, ie. if you're trying to do the same for say .swf files there will be problems. Firefox doesn't run the .swf file if it's not visible.
当我们想提前为 CSS 加载图像时,这对我来说最有效
(而 rel="prefetch" 会导致 CSS 重复加载)
This works best for me when we want to load image early for CSS
(while rel="prefetch" will cause duplicate loading from CSS)
如果您想要预加载图像,那么性能就是您想要的。 我怀疑在资源加载时阻塞页面是您想要的。 因此,只需在正文末尾放置一些预取链接,然后将虚假媒体添加到其中,使它们显得不重要(以便它们在其他所有内容之后加载)。 然后,将 onload 标记添加到这些链接,其中 onload 将是设置页面中实际资源的源的代码。 例如,这甚至可以与动态 iframe 结合使用。
前:
后:
请注意,第一个(之前)如何冻结页面并以一种非常丑陋的方式闪烁,而预加载内容的第二个(之后)不会冻结页面或闪烁,而是立即无缝地出现和消失。
If preloading images is what you seek, then performance is what you want. I doubt blocking up the page while the resources load is what you want. SO, just place some prefetching links at the end of the body and add the bogus media to them to make them appear unimportant (so that they get loaded after everything else). Then, add the onload tag to those links, and in that onload will be the code that sets the source of the actual resource in your page. For example, this could even be used in conjunction with dynamic iframes.
Before:
After:
Notice how the first one (before) freezes up the page and blinks in a quite ugly manner while the second one (after) with the content preloaded doesn't freeze up the page or blink, rather it appears and disappears seamlessly instantly.
在不可见的 img 标签中引用您的图像。 页面加载时它们也会下载。
Reference your images in invisible img tags. while page loading they will downloaded too.
由于我不确定是否加载隐藏图像,您可能需要使用图像精灵。 然后在显示任何内容之前加载整个图像。 您甚至可以将所有菜单项放在一张图像中,从而节省大量 HTTP 请求。
As I'm not sure if hidden images are loaded, you'll probably want to use image sprites. Then the entire image is loaded before anything is displayed. You can even put all of your menu items in one image and therefore save a lot of HTTP requests.
您不能将它们作为
标记添加到您的页面并使用 css 隐藏它们吗?
Can't you add them as an
<img />
tag to your page and hide them using css?