我应该在网站上使用类似精灵的缩略图技术吗?

发布于 2024-08-17 19:19:24 字数 454 浏览 3 评论 0原文

在我正在创建的网站上,我有大约 100 个不同的缩略图 (64x64),它们在不同的时间显示。在某些页面上,可能仅显示 5-15 个缩略图。在其他情况下,所有 100 个都已加载。

我正在考虑使用 CSS sprites 等技术来显示图像。也就是说,不要为每个拇指添加图像标签,而是执行以下操作:

<span class=thumb1"></span>

然后使用 CSS 拍摄一张图像的切片,并将所有拇指缝合在一起。也就是说,一张图像包含所有 100 个拇指(在本例中为 640x640 图像)。

我的问题:

  • 这是个好主意吗?
  • 如果是, 我应该在所有页面上这样做吗 图像出现,或仅出现在页面上 与所有图像?
  • 有没有 除了精灵之外的另一种技术 这可能比简单地更好 包括带有 img 标签的图像?

On a website I'm creating, I have about 100 various thumbnails (64x64) that get displayed at different times. On some pages, only 5-15 thumbnails may be displayed. On others, all 100 are loaded.

I'm considering using a technique like CSS sprites to display the images. That is, rather than have image tags for each thumb, do something like:

<span class=thumb1"></span>

And then use CSS to take a slice of one single image with all the thumbs stitched together. That is, one image with all 100 thumbs (in this scenario, a 640x640 image).

My questions:

  • Is this a good idea?
  • If yes,
    should I do it on all pages the
    images occur, or only on the pages
    with all the images?
  • Is there
    another technique other than sprites
    that may be better than simply
    including the images with img tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七度光 2024-08-24 19:19:24

如果您认为普通用户会使用这些缩略图访问至少两个不同的页面,那么我的观点是使用精灵确实是一个好主意

事实上,我会用所有缩略图制作一个大图像,然后在所有页面中使用它!

为什么?

  • http 请求更少(从 100 减少到 1)!这是网站优化最重要的事情之一(请阅读 Yahoo Performance Team 速度优化规则
  • 这样用户将仅在第一次下载整个图像,然后他们将在以下所有页面中快速加载该图像
  • 互联网上最著名的网站已经这样做了!请参阅 Yahoo 中使用的精灵,亚马逊Youtube 页面。
  • 您可以将其他 UI 或布局图像添加到精灵中

优化生成的 PNG:

生成精灵后,如果是 PNG,您可以使用此工具进一步优化 PNG:http://www.sitepoint.com/blogs/2009/09/18/ squishing-the-last-drops-from-your-pngs/

何时不使用精灵:

  • 当精灵中的部分图像频繁更改时
  • 在这种特定情况下:当大多数用户需要少于 (大约)10%的图像

If you think that an ordinary user would visit at least two different pages with these thumbnails than my opinion is that using sprites would really be a good idea!

I would in fact make a single big image with all the thumbnails and then use it in all the pages!

Why?

  • Fewer http requests (from 100 to 1)! And this is one of the most important thing about web site optimizations (read here from Yahoo Performance Team speed optimization rules )
  • This way users will download the whole image only the first time and then they will get a super quick loading of that images in all the following pages
  • The most famous websites on the internet already do that! See sprites used in Yahoo, Amazon or Youtube pages.
  • You can add other UI or layout images to your sprite

Optimize the resulting PNG:

After you have generated your sprite, if a PNG, you can optimize the PNG even more using this tool: http://www.sitepoint.com/blogs/2009/09/18/squishing-the-last-drops-from-your-pngs/

When not to use sprites:

  • When part of the images in the sprite change frequently
  • In this specific case: when the majority of users would need less than the (about) 10% of the images
遗忘曾经 2024-08-24 19:19:24

我不太清楚你所说的“精灵”是什么意思,但这就是我认为你的意思:你创建了 1 个带有 10x10 光栅的图像,而不是单独的 100 个图像。每个坐标 (x,y) 包含您想要显示的图像之一。
现在,如果您显示图像,则使用 CSS 来设置背景位置:即 x * -64pxy * -64px,也许使用 JavaScript 来计算 x 和y 对于每个图像范围,或服务器端脚本来打印动态 CSS。

  • 是的,这是一个好主意:它减少了加载时间,因为只需下载一张大图像,而不是数百个较小的图像。
  • 这取决于。如果您具有页面缓存功能,则可以将所有缩略图“缝合”到一个图像文件中。如果您有一个非常动态的网页,那么每次更新缩略图时构建这个拼接图像是相当困难的。
  • 不确定,如果这就是您用“精灵”表示的内容,那么不,如果您用“精灵”表示其他内容,那么是的:这个答案就是一个例子。

I'm not quite sure what you mean with "sprites", but this is what I think you mean: instead of 100 images seperately, you create 1 image, with a 10x10 raster. Each coordinate (x,y) contains one of the images you like to show.
Now, if you display an image, you use CSS to set background-location: i.e. x * -64px and y * -64px, perhaps using JavaScript to calculate the x and y for each image-span, or Server-Side scripting to print out a dynamic CSS.

  • Yes, this is a good idea: it reduces load time, since one only has to download one big image, instead of hundreds of smaller ones.
  • It depends. If you have caching-abilities for a page, then you can "stitch" all thumbnails into one image file. If you have a very dynamic webpage, then it's quite harsh to build this stitched image every time the thumbnales are updated.
  • Not sure, if this is what you ment with "sprites", then no, if you ment something else with "sprites", then yes: this answer is an example of one.
眉黛浅 2024-08-24 19:19:24

如果

  • 速度很重要

  • 你不关心可访问性(屏幕阅读器阅读图像的 ALT 文本等,当你使用精灵时所有这些都消失了)< /p>

  • 你不关心你的缩略图不会被打印任何浏览器中的默认设置

  • 你可以做到这一点,而不会成为维护的噩梦(哪个图像又出现在位置 461 上?)

至于你的第二个问题,建议将所有精灵放入一个或很少的容器图像中,以最大限度地减少加载时间。

It is a good idea if

  • Speed matters

  • You don't care about accessibility (screen readers reading the ALT text of an image, etc, all that is gone when you use sprites)

  • You don't care that your thumbnails are not going to be printed by default in any browser

  • You can do it without it becoming a maintenance nightmare (which image was on position 461 again?)

As to your second question, it is probably advisable to put all sprites into one or very few container images to minimize loading times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文