简体   繁体   English

精灵与图像切片

[英]sprites vs image slicing

I don't have much experience with the sprite approach to images ( http://www.alistapart.com/articles/sprites ). 我对图像的精灵方法没有太多经验( http://www.alistapart.com/articles/sprites )。 Anyone care to share some pros/cons of sprites vs. old-school slices? 有人愿意分享精灵与老派切片的优点/缺点吗?

The main advantage of sprites is that the browser has to request less pictures from the webserver. 精灵的主要优点是浏览器必须从网络服务器请求更少的图片。 That reduces the number of HTTP requests and makes it possible to compress the parts of the design more effectively. 这减少了HTTP请求的数量,并且可以更有效地压缩设计的各个部分。 These two points also represent the disadvantages of sliced images. 这两点也代表了切片图像的缺点。

Here you can see some good examples how sprites improve the loading speed of web pages: 在这里你可以看到一些很好的例子,精灵如何提高网页的加载速度:

http://css-tricks.com/css-sprites/ http://css-tricks.com/css-sprites/

Pros: 优点:

  • It's far easier on the server to serve a single large image than many small ones. 服务器上服务单个大图像要比许多小图像容易得多。
  • It's (slightly) faster for a web browser to load such an image. Web浏览器加载这样的图像(稍微)更快。
  • Browsers only load images as they needs them - if you are using multiple images in a rollover, the browser would "pause" the first time you roll over the element. 浏览器仅在需要时加载图像 - 如果您在翻转中使用多个图像,浏览器将在您第一次翻转元素时“暂停”。 This can be solved using sprites, because there is only one image to load. 这可以使用精灵来解决,因为只能加载一个图像。

Cons: 缺点:

  • It's kind of a pain to code (more so than using multiple images at least) 编码很麻烦(至少比使用多个图像更多)

One often overlooked downside of using CSS sprites is memory footprint: 一个经常被忽视的使用CSS sprites的缺点是内存占用:

https://web.archive.org/web/20130605000516/http://blog.vlad1.com/2009/06/22/to-sprite-or-not-to-sprite/ https://web.archive.org/web/20130605000516/http://blog.vlad1.com/2009/06/22/to-sprite-or-not-to-sprite/

Unless the sprite image is carefully constructed, you end up with incredible amounts of wasted space. 除非仔细构建精灵图像,否则最终会产生大量浪费的空间。 My favourite example is from WHIT TV's web site, where this image is used as a sprite. 我最喜欢的例子来自WHIT TV的网站, 这个图像用作精灵。 Note that this is a 1299×15,000 PNG. 请注意,这是一个1299×15,000 PNG。 It compresses quite well — the actual download size is around 26K — but browsers don't render compressed image data. 它压缩得很好 - 实际下载大小约为26K - 但浏览器不会渲染压缩的图像数据。 When this image is downloaded and decompressed, it will use almost 75MB in memory (1299 * 15000 * 4). 下载并解压缩此图像时,它将使用大约75MB的内存(1299 * 15000 * 4)。

When sprites get loaded into the browser, they are stored uncompressed. 当精灵加载到浏览器中时,它们将以未压缩的形式存储。 A 26 KB file can uncompress to take up a whopping 75 MB of RAM. 一个26 KB的文件可以解压缩以占用高达75 MB的RAM。 You should be mindful of using sprites with very large dimensions. 你应该注意使用尺寸非常大的精灵。

There's also the issue of what happens in browsers with poor CSS support (legacy browsers). 还存在CSS支持较差的浏览器(旧版浏览器)中发生的问题。 The sprites may end up totally broken. 精灵最终可能完全崩溃。

Sprites 精灵

Pros: 优点:

  • Less HTTP connections to the server 减少与服务器的HTTP连接
  • Faster loading on broadband 宽带加载速度更快

Cons: 缺点:

  • No encapsulation: If you want to change one image, you have to change the sprite 无封装:如果要更改一个图像,则必须更改精灵
  • It is difficult to setup individual images in CSS without a tool 没有工具很难在CSS中设置单个图像
  • Don't degrade: If the browser don't support CSS, you are in trouble 不要降级:如果浏览器不支持CSS,则会遇到麻烦

CSS Sprites: CSS Sprites:

Pros: 优点:

  • Graceful degrade in unsupported browsers (text can be shown when background images for links are not allowed) 在不支持的浏览器中优雅降级(当不允许链接的背景图像时可以显示文本)
  • Fewer HTTP requests 更少的HTTP请求
  • Each image has a separate overhead like color table so image slicing will be having more overhead than CSS sprites 每个图像都有一个单独的开销,如颜色表,因此图像切片将比CSS精灵具有更多的开销

Cons: 缺点:

  • Poses a problem if images are turned off in the browsers (rare case though) 如果在浏览器中关闭图像则会出现问题(但很少见)

Image slicing: 图像切片:

Pros: 优点:

  • User perceives a faster load since loaded piece by piece. 由于逐件装载,用户感觉到更快的负载。
  • Load on demand like when the user places his mouse on the image 按需加载,例如当用户将鼠标放在图像上时

Cons: 缺点:

  • The webpages might have a large size on the client side even thought it might not be the case on the server side. 即使在服务器端可能不是这种情况,网页也可能在客户端具有较大的大小。

Look into using a CSS sprite generator (we use SmartSprites ). 考虑使用CSS sprite生成器(我们使用SmartSprites )。 That way you can do slices locally, and have your build process generate a spritemap. 这样你就可以在本地做切片,让你的构建过程生成一个spritemap。 It's the best of both worlds. 这是两全其美的。

Also is SmartSprites isn't for you, there's definitely others, however I like it because it reduces the amount of work up front AND during changes. 另外SmartSprites不适合你,肯定是其他人,但我喜欢它,因为它减少了前期和更改期间的工作量。

Cons - slower on older browsers/ maybe not working on them with hover effect (opera6) - if not used correctly can get very/too huge (group them adequately!) - tedious work to set them up 缺点 - 旧浏览器速度较慢/可能没有悬停效果(opera6) - 如果使用不当可能会变得非常/太大(将它们充分分组!) - 设置它们的繁琐工作

Pros - less bytes transfered, because one big image is smaller then all individual images combined (one header/ color table) - less http requests 优点 - 更少的字节传输,因为一个大图像小于所有单个图像组合(一个标题/颜色表) - 更少的http请求

The main drawback of sprites is it makes it hard to read/maintain/modify your CSS. 精灵的主要缺点是它很难读取/维护/修改你的CSS。 It can be difficult to remember the exact pixel offsets within the sprite. 可能很难记住精灵内的精确像素偏移。

pros using sprites : since it is using 1 images for all, it require less load on http server. 使用精灵的专业人士:由于它使用的是1张图像,因此在http服务器上需要较少的负载。

cons: - hard to code. 缺点: - 难以编码。 you must know the coordinate each images inside sprites so you can display it correctly. 你必须知道sprite中每个图像的坐标,这样你才能正确显示它。 once you change the size of the image, you need to adjust all ... - big images could creates long waited page to display. 一旦你改变了图像的大小,你需要调整所有... - 大图像可以创建长等待页面来显示。 while using images, user with slow internet connection can see one by one. 在使用图像时,具有慢速互联网连接的用户可以逐个查看。

best practices. 最佳做法。 use it for example roll over images. 使用它例如翻转图像。

I prefer going the middle ground of grouping similar images (normal, hover, selected page, the parent page of selected page) than having all the images in one file. 我更倾向于将相似图像(正常,悬停,选定页面,所选页面的父页面)分组,而不是将所有图像分组到一个文件中。 To make these, you image slice like normal in Photoshop or Illustrator, open the files up and combine them with a shortcut key. 要制作这些,您可以在Photoshop或Illustrator中像普通图像一样切片,打开文件并将它们与快捷键组合在一起。 I wrote the Photoshop script that combines images into CSS sprites . 我编写了将图像组合成CSS精灵的Photoshop脚本。 You will have multiple HTTP connections, but won't have the load delay on hover. 您将拥有多个HTTP连接,但悬停时不会出现负载延迟。

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

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