简体   繁体   English

为什么不动画GIF而不是动画CSS精灵?

[英]Why not animated GIF instead of animated CSS sprites?

In recent trends I've seen people animating CSS sprites using JavaScript instead of using animated GIFs? 在最近的趋势中,我看到人们使用JavaScript而不是使用动画GIF动画CSS精灵?

Ex: 例如:

Is that all just to show or experiment with technology or are there any benefits out of it. 这只是为了展示或试验技术,还是有任何好处。 I m interested in knowing the benefits, if there. 我有兴趣了解它的好处,如果有的话。 The reason I m asking is that I couldn't figure out as in both cases we need to generate the intermediate frames (mostly using tweening technique). 我问的原因是我无法弄清楚在两种情况下我们都需要生成中间帧(主要是使用补间技术)。

  • Control 控制

    You have no control over animated GIFs. 您无法控制动画GIF。 You can't start them, you can't stop them. 你无法启动它们,你无法阻止它们。 They just animate as soon as they load. 它们只要加载就会生成动画。

    With sprites, you can control the animation. 使用精灵,您可以控制动画。 You can start, stop and react to browser events, pan through the animation. 您可以启动,停止和响应浏览器事件,平移动画。 For example, Google Doodles usually activate when you click on them. 例如,Google Doodles通常会在您点击它们时激活。

    A nifty GIF control system can be found in the 9gag. 9gag中可以找到一个漂亮的GIF控制系统。 You can start them by appending them to the DOM, and stop them by removing them and swapping them with a pre-generated "first-frame view". 您可以通过将它们附加到DOM来启动它们,并通过删除它们并使用预先生成的“第一帧视图”交换它们来停止它们。 But that's as far as GIFs go. 但这就像GIF一样。

  • Independent Instances 独立实例

    When you load multiple instances of the same GIF, all these instances use the same image across the page and move at the same time. 当您加载同一GIF的多个实例时,所有这些实例在整个页面中使用相同的图像并同时移动。 If you have a row of dancing unicorns GIFs, they'd be dancing at the same time. 如果你有一排舞蹈独角兽GIF,他们会在同一时间跳舞。 Synchronized dancing! 同步跳舞!

    But with sprites, even if you are using the same images, the animation relies on the underlying script. 但是对于精灵,即使您使用相同的图像,动画也依赖于底层脚本。 So if one sprite is animated by one script and another by another script, both animations can run independently, and differently from one another. 因此,如果一个精灵由一个脚本激活而另一个精灵由另一个脚本激活,则两个动画可以独立运行,并且彼此不同。

    This saves you from creating another GIF and it's easy to modify since you are only changing the script. 这样您就不会创建另一个GIF,而且只需更改脚本就可以轻松修改。

  • Ensuring smooth animation 确保流畅的动画

    Animated GIFs just animate while loading, and when the internet is slow, the animations freeze up until more of the image gets loaded. 动画GIF在加载时只是动画,当互联网很慢时,动画会冻结,直到更多的图像被加载。

    In contrast, the advantage of sprites is you can pre-load them, ensure all images load beforehand. 相比之下,精灵的优势在于您可以预先加载它们,确保预先加载所有图像。 This makes sure that the resources used for that animation are already loaded prior to animation to make sure it animates as smooth as possible. 这可确保在动画之前已加载用于该动画的资源,以确保其动画尽可能平滑。

    However, GIFs are still images. 但是,GIF仍然是图像。 You can dynamically load them off the DOM and listen for a load event before you append them to the DOM. 您可以动态地将它们从DOM中加载并在将它们附加到DOM之前侦听加载事件。

  • Partial rendering 部分渲染

    With PNG sprites, you can do "partials" in the animation, breaking an animation scene to parts. 使用PNG精灵,您可以在动画中执行“部分”操作,将动画场景分解为部分。 For example, when a character stands still, but the arms are waving. 例如,当一个角色静止不动时,手臂正在挥动。 You don't need to animate the entire character, or the entire scene. 您无需为整个角色或整个场景设置动画。 You can place an element depicting the sprite of the character's body in a "freeze" state while the arms are a different element that is animating. 您可以将描绘角色身体精灵的元素置于“冻结”状态,而手臂则是动画的不同元素。 This conserves space and size of the sprite sheet. 这节省了精灵表的空间和大小。 A good example for this was the 2012 Mother's Day Doodle by Google. 一个很好的例子就是Google 2012年的母亲节涂鸦。

    In contrast, most of the time , every frame in a GIF animation is whole image, and animates whether or not anything in it moves. 相比之下, 大多数情况下 ,GIF动画中的每一帧都是整个图像,并动画显示其中的任何内容是否移动。 The more frames, the bigger the size of the GIF. 帧越多,GIF的大小越大。

  • GIFs just don't scale GIF只是不扩展

    GIFs were meant for icons. GIF用于图标。 The ratio of file size to image size don't scale up that well in GIFs as compared with PNG and JPG. 与PNG和JPG相比,GIF中文件大小与图像大小的比例不能很好地扩展。

On top of Joseph the Dreamer's answer... 在约瑟夫之上,梦想家的答案......

As far as I know, or atleast it used to be that, GIF files are NOT true colour, another reason to use a JPGs/PNGs as a css sprite. 据我所知,或者至少它曾经是那样,GIF文件不是真正的颜色,是使用JPG / PNG作为css精灵的另一个原因。

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

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