JQuery - 如何平滑地调整图像大小以创建脉动效果?
嘿,我和我的一个朋友在一个网站上工作,从一开始,我们的客户就对我们的一个动画的质量不满意。该动画获取图像并将其放大,然后将其缩小,并重复以获取脉动效果。整个动画中不透明度也会发生变化。
当前动画位于网站 http://laveryrowe.com 的主页上。所讨论的动画是您到达网站后立即可以看到的 75% 的图像。
我已经在 safari、firefox 和 internet explorer 中进行了测试。动画只能在 Firefox 中进行剪辑,但是 Safari 和 Internet Explorer 无法为我们的客户提供足够平滑的调整大小。
有谁知道比我使用的方法更好的动画方法? (请参阅下面的代码并查看该网站的示例)。
function pulse() {
$('#seventyfive').animate({
marginTop: 175,
marginLeft: 25,
width: 261, height: 98,
opacity: 0.5
}, 700, function() {
$('#seventyfive').animate({
marginTop: 161.95,
marginLeft: 15.2,
width: 287.1, height: 107.8,
opacity: 1
}, 700, function() {
pulse();
});
});
};
预先非常感谢,我们真的需要一只手,
编辑: 问题不在于位置(或者至少我不认为如此),而更多地与图像调整大小的方式有关,当它变大时,你会注意到边缘的抖动。随着不透明度的增加,它看起来看起来更好,但当它不透明时我需要相同的质量。
Jai
Hey, Im working on a website with a friend of mine, and from the start, our client has been unhappy with the quality of one of our animations. This animation takes an image and makes it larger, it then makes it smaller, and repeats to get a pulsating effect. The opacity also changes throughout the animation.
The current animation is on the home page of the site http://laveryrowe.com. The animation in question is the 75% image that you can see immediately upon arriving at the site.
I have tested in safari, firefox and internet explorer. The animation only just about makes the cut in firefox, however safari and internet explorer do not produce smooth enough resizes for our client.
Does anyone know of a better method of animating than the one i have used? (see code below and check out the site for an example).
function pulse() {
$('#seventyfive').animate({
marginTop: 175,
marginLeft: 25,
width: 261, height: 98,
opacity: 0.5
}, 700, function() {
$('#seventyfive').animate({
marginTop: 161.95,
marginLeft: 15.2,
width: 287.1, height: 107.8,
opacity: 1
}, 700, function() {
pulse();
});
});
};
Many thanks in advance, We could really use a hand,
Edit: The issue isn't with the positioning, (or at least i don't think it is) instead, its more to do with the way the image is resized, you can notice the jittery edges as it gets bigger. It seems to look better as the opacity is increased, but i need the same quality for when it is opaque.
Jai
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的动画不平滑,因为您的 marginLeft 向下舍入(图像向左移动一个像素),然后您的宽度向上舍入(图像像素向右移动一点,因为它们被重新采样到更大的宽度。)尽管图像没有向右移动,但您的眼睛会告诉您它已经向右移动,因为它们认为图像的中间稍微向右移动。这与垂直执行相同的操作一起使动画看起来在跳跃。
以下是我认为边缘似乎闪烁或摇晃的原因的示例。下面是两张 3 x 1 像素的图像。它们的大小都调整为 5 x 1 并向左移动 4 个像素。蓝色的是您所看到的,其中大小和位置独立变化。红色的只允许在位置改变时改变大小,并且应该看起来是一个平滑的动画。
Your animation isn't smooth because your marginLeft gets rounded down (image moves to the left one pixel) and then your width gets rounded up (image pixels are moved right just a little because they are re-sampled to a larger width.) Even though the image didn't move right your eyes tell you that it did because they perceive the middle of the image as being slightly to the right. This along with doing the same thing vertically makes the animation appear to jump around.
Here's an example of why I think the edges seem to flicker or shake. Below Are two images both 3 by 1 pixels. They are both resized to 5 by 1 and moved 4 pixels to the left. The blue one is what you are seeing where the size and location change independently. The red one only allows the size to change when the location changes and should appear to be a smooth animation.
您研究过使用 jQuery UI 效果吗?
http://jqueryui.com/demos/effect/#default
另一种选择是与他们交谈摆脱了75%的脉动,进入了一些黄色闪烁的横幅和有品味的框架使用。
Have you looked into using jQuery UI effects?
http://jqueryui.com/demos/effect/#default
Another alternative is to talk them out of the pulsing 75% and into some yellow blinking banners and the tasteful use of frames.
那么如何设置字体大小的动画呢?即=> http://jsfiddle.net/steweb/D3X7R/
js/jQuery
标记:
Css:
And what about animating the font size? I.e. => http://jsfiddle.net/steweb/D3X7R/
js/jQuery
Markup:
Css:
您可以尝试使用 += 某个数量,而不是每次都动画到特定值。我以前用过它,从未发现任何问题。
You might try using += some quantity instead of animating to a specific value each time. I've used it before and never noticed any problems.
这是以下示例 →
问题是您正在为两个同时调整位置和大小,并且它们彼此不同步。我知道您这样做是为了使其垂直和水平居中。相反,我会使用一个(可怕的)表格来将图像保持在正确的位置,然后用 Javascript 调整大小和不透明度,如下所示:
您可以根据需要定位和调整此表格的大小它位于正确的位置(如有必要,使用绝对定位)。
查看示例→
Here's an example of the following →
The problem is that you're animating both the position and the size at the same time and they're out of sync with each other. I understand that you're doing this to try to keep it centered vertically and horizontally. Instead, I would use a (dreaded) table to keep the image in the correct position and just pulse the size and opacity with Javascript, like so:
You can position and size this table however you need to get it in the right place (using absolute positioning if necessary).
See example →
我会使用 Flash 为“75%”PNG 制作超级平滑的动画,并具有缓动功能。这会非常有效。
I would use Flash to animate the "75%" PNG SUPER smooth with easing function. That will work very well.