简体   繁体   English

优化javascript预加载图像

[英]Optimize javascript pre-load of images

I was wondering if anyone has any strategies for optimizing the pre-loading of images via javascript? 我想知道是否有人有任何优化通过javascript预加载图像的策略?

I'm porting a Flash application into html/css, attempting to recreate the UI as close to the original site as possible. 我正在将Flash应用程序移植到html / css中,尝试尽可能靠近原始站点重新创建UI。 It's essentially a photo browser application, where a high-res image is shown when the user hovers over a link. 它本质上是一个照片浏览器应用程序,当用户将鼠标悬停在链接上时会显示高分辨率图像。 There's about 50-80 of these images per page. 每页大约有50-80个这样的图像。

Pre-loading all the images individually creates a load time significantly longer than that of the Flash application. 单独预加载所有图像会产生比Flash应用程序更长的加载时间。 The amount of data is the same, but I have to assume the longer load time is due to the number of round trips that have to be made to the server for each image. 数据量是相同的,但我必须假设更长的加载时间是由于每个图像必须对服务器进行的往返次数。

Also, I'm looking at a significant load time for each page even after the images have been cached, because the page still needs to contact the server for each image to receive the 304 Not Modified code. 此外,即使在缓存了图像之后,我也在查看每个页面的显着加载时间,因为页面仍然需要联系服务器以获取每个图像以接收304 Not Modified代码。

Does anyone have any suggestions for speeding this up? 有没有人有任何加快这个的建议? Would it make sense to possibly try creating one huge image sprite that gets downloaded in a single request rather than 50-80 smaller images that each take a single request? 可能尝试创建一个可以在单个请求中下载的巨大图像精灵而不是每个只需要一个请求的50-80个较小图像是否有意义? The goal here is achieve a similar load time to the Flash site. 这里的目标是实现与Flash站点类似的加载时间。

Thanks. 谢谢。 I know this doesn't sound like an ideal way to do things. 我知道这听起来不是理想的做事方式。

As you pointed out - one of the most common factors that influences load times for modern web pages are the huge number of small images being sent between the client and server. 正如您所指出的 - 影响现代网页加载时间的最常见因素之一是客户端和服务器之间发送的大量小图像。 Some recent studies indicate that for more than 20 images, 80% of your load time is going to be spent in the latency of doing these many gets, not actually doing the downloading! 最近的一些研究表明,对于超过20个图像,80%的加载时间将花费在执行这些许多获取的延迟上,而不是实际进行下载!

A tool called SmartSprites is a very handy way to "compile" a single large image from all of your many little images which will accomplish both much faster load times and the prefetching of images. 一个名为SmartSprites的工具是一种非常方便的方法,可以从所有的小图像中“编译”单个大图像,从而实现更快的加载时间和图像的预取。 Here's the link http://smartsprites.osinski.name/ 这是链接http://smartsprites.osinski.name/

I would say using CSS sprites is a big one. 我会说使用CSS sprites是一个很大的问题。 Having all your images, or images of a similar nature come down on the one HTTP request really helps load time. 将所有图像或类似性质的图像放在一个HTTP请求上真的有助于加载时间。 Downloading 10 images with a total file size of 100kb is going to be slower than downloading 1 image the same size. 下载总文件大小为100kb的10个图像将比下载相同大小的1个图像慢。 It also helps images come down before they are required in the case of hovers and mouse over events and they generally make things a lot smoother. 在徘徊和鼠标悬停事件的情况下,它还可以帮助图像在需要之前降下来,并且它们通常会使事情变得更加顺畅。

Here is an article on them. 这是关于它们的文章。

Using a sprite is good if you're OK with that solution. 如果您对该解决方案没问题,使用精灵是很好的。 Also, what I'd try is to detect data URI support in that browser and have the script load different stylesheets base on that info. 此外,我尝试的是检测该浏览器中的数据URI支持,并让脚本根据该信息加载不同的样式表。 One stylesheets with base 64 encoded data URIs and the other with a real URL to the sprite. 一个样式表具有基本64位编码数据URI,另一种样式表具有精灵的真实URL。 The CSS file is also cacheable. CSS文件也是可缓存的。

Here's a technique to detect data URIs support or you could use IE conditional comments or... there are some workarounds for sending IE encoded image strings , in multipart HTTP responses. 这是一种检测数据URI支持技术,或者您可以使用IE条件注释或...在多部分HTTP响应中有一些解决方法用于发送IE编码图像字符串

Use Sprite Me . 使用Sprite Me And don't miss the blog posts about the creation of this tool. 并且不要错过有关创建此工具的博客 文章

If you don't need all the images right away, you can use the window.timeout() function to do some of the work after the page has initialized. 如果您不需要立即显示所有图像,则可以使用window.timeout()函数在页面初始化后执行一些工作。

Also, if you use one image in multiple places, you can avoid multiple calls to the server by putting the image into a CSS style and reusing that style instead of the image. 此外,如果您在多个位置使用一个图像,则可以通过将图像放入CSS样式并重复使用该样式而不是图像来避免多次调用服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM