简体   繁体   English

如何使Javascript / jQuery动画更好?

[英]How do I make this Javascript/jQuery animation better?

Webpage is - http://schnell.dreamhosters.com/spriteanimate.html 网页是-http: //schnell.dreamhosters.com/spriteanimate.html

Just go to the page and 'View Source' and you'll see all the code. 只需转到该页面并单击“查看源代码”,您将看到所有代码。

Right now it's pretty simple. 现在很简单。 Right arrow key makes X go right, left arrow key makes him go left. 右箭头键使X右移,左箭头键使他左移。 He does a running animation in the proper direction and the actual image element moves in the same direction. 他在正确的方向上运行动画,而实际的图像元素在相同的方向上运动。

The major issue I have with this right now is optimization. 我现在遇到的主要问题是优化。 If you play around with it a little you'll notice that the running animation isn't always smooth and for the first few seconds it feels very laggy, like it's still loading something. 如果您稍微玩一下它,您会注意到正在运行的动画并不总是很流畅,并且在开始的几秒钟内感觉非常缓慢,就像它仍在加载某些东西一样。 The animation for going left is particularly guilty of this. 左转的动画对此特别内。 Another problem is with the .animate() from jQuery. 另一个问题是来自jQuery的.animate()。 It does a sort of stop-and-go motion and you can tell. 它可以执行停停走走的动作,您可以看出。 The movement of the image element is jerky and not very smooth. 图像元素的移动非常不稳定,并且动作不流畅。

The .animate() from jQuery can be tweaked with easing and such, but I'm not entirely sure if that's the answer and that still leaves me with the occasional running lag or something. jQuery的.animate()可以通过缓动进行调整,但是我不确定是否是答案,并且仍然使我偶尔遇到运行时滞等问题。 So does anyone have any suggestions? 那么有人有什么建议吗?

Use image preloading for the sprite images, or better still, use one image as a sprite, and instead of swapping the image file, shift the position of the image file. 对sprite图像使用图像预加载,或者更好的是,将一个图像用作sprite,而不是交换图像文件,而是移动图像文件的位置。 You need to set the image as a background image and shift the background position to achieve this. 您需要将图像设置为背景图像并移动背景位置以实现此目的。

Also, use linear easing, otherwise it will default to swing, which enters and exits slowly. 另外,请使用线性缓动,否则它将默认为摆动,缓慢进入和退出。

Have you considered using CSS3 transitions rather than standard jQuery animations. 您是否考虑过使用CSS3过渡而不是标准jQuery动画。 In doing so you'll have dramatically better performance but lose some cross browser compatibility. 这样做会大大提高性能,但会失去一些跨浏览器的兼容性。 Here's an example which only works in Chrome but shows the power 这是一个仅在Chrome中运行但显示出强大功能的示例

http://girliemac.com/sandbox/matrix.html http://girliemac.com/sandbox/matrix.html

and more about CSS3 animations 还有更多关于CSS3动画的信息

http://css3.bradshawenterprises.com/ http://css3.bradshawenterprises.com/

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

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