jQuery animate() ...每步几个像素
我正在尝试构建一个类似控制台的动画。
我想使用 animate
函数来调整 div 的大小,但不能在平滑的动画中,例如每步 1 像素。我想要每步 10 像素左右。
我在 animate
函数中找不到解决方案或选项,因此我尝试使用 animate 的步骤选项,但它不起作用:
如下所示:
$(this).animate({height: "60"}, {
duration: 5000,
step: function(){
var curheight = $(this).height();
$(this).css('height', curheight+9+'px');
}
});
animate 仍然将其动画化为一步 1 像素并忽略新的高度。
有什么想法吗?我有点被困在这里了。
谢谢。
I am trying to build a console-like animation.
I wanted to use the animate
function to resize a div, but not in a smooth animation like 1px per step. I want more like 10px per step.
I could not find a solution or an option within the animate
function, so i tried to use the step-option of animate, but it doesn't work:
Something like this:
$(this).animate({height: "60"}, {
duration: 5000,
step: function(){
var curheight = $(this).height();
$(this).css('height', curheight+9+'px');
}
});
animate
still animates it 1 pixel a step and ignores the new height.
Any ideas? I'm kind of stuck here.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您使用默认值制作动画时:
$(this).animate({height: "60"})
它变成一个
swing
动画,如下所示:$(this).animate({height: "60"}, 500, "swing")
现在 默认情况下可用的缓动选项有
swing
和linear
。听起来您想要一个名为stepped
的新功能。查看 jQuery 源代码.. 这是它如何添加缓动方法的开始...
在互联网上查找,您可以使用命令查看动画
现在我真的不知道
fn.extend< /code> 东西,因为当我尝试它时它甚至不起作用......但无论如何我尝试了这个并且它起作用了。 (只是做与线性相同的操作)
在这里尝试一下 http://jsfiddle.net/UFq7c/1/
好像参数是
p
完成百分比n
发生的毫秒数firstNum
起点diff
多远走向线性很容易弄清楚。起点加上要走多远的时间 完成百分比
我们可以轻松地说一次移动百分之十,而不是一次移动百分之十分之一 在这里
试试 http://jsfiddle.net/UFq7c/2/
现在唯一的挑战是将其转换为像素数。您可能会认为
firstNum
为零,diff
为 200.. 但是 nooo..firstNum
似乎始终为零百分比,而diff
始终为 100%(百分百是第一)。唔。看起来很傻。 0% 加 100% 次完成百分比...哦好吧
好吧,看起来您一次只需制作一定数量的动画。通过上面的示例,您可以轻松地一次为 10 个像素设置动画,只需使用此
100 像素动画两次,一次 10%(100 像素的 10% 就是一次 10 个像素)。
您可能想要将 10% 更改为 100%,然后像这样一次为 10 个像素设置动画,
这取决于您的代码如何工作。 100% 规则看似愚蠢,但却有效。
如果您使用 100% 规则,那么您可能希望像这样缩短它,这会产生相同的结果
现在我将等待我的答案被否决,因为我不在我的 if 语句中使用大括号,也不在我的 if 语句末尾使用分号命令。
干杯!
When you do an animation with the default:
$(this).animate({height: "60"})
It turns in to a
swing
animation like this:$(this).animate({height: "60"}, 500, "swing")
Now the easing options that are available by default are
swing
andlinear
. It sounds like you want a new one calledstepped
.Looking at the jQuery source code.. here is how it adds the easing methods to begin with...
Looking on the internet you can see the animations with the command
Now I really don't know about that
fn.extend
stuff because it did not even work when I tried it... but anyway I tried this and it worked. (just doing the same as linear)Try it here http://jsfiddle.net/UFq7c/1/
It seems that the parameters are
p
Percentage completionn
Number of milliseconds transpiredfirstNum
The starting pointdiff
How far to goLinear was easy to figure out. Starting point plus How far to go times Percentage completion
We can easily say to move ten percent at a time instead of a tenth of a percent at a time with this
Try it here http://jsfiddle.net/UFq7c/2/
Now the only challenge is turning that into a number of pixels. You would think that
firstNum
would be zero anddiff
would be 200.. but nooo..firstNum
seems to always be zero percent anddiff
is always 100% (one hundred percent is the number one).hmm. Seems silly. 0% plus 100% times Percentage complete... oh well
Well it looks like you will have to animate only a certain amount at a time. You can easily animate ten pixels at a time with the above example just using this
That animates 100 pixels twice, 10% at a time (10% of 100 pixels is 10 pixels at a time).
You may want to change 10% to 100% and then animate 10 pixels at a time like this
It depends on how your code works. The 100% rule seems silly but it works.
If you use the 100% rule then you may want to make it shorter like this which makes the same result
Now I will wait for my answer to be down voted because I do not use braces on my if statements or semicolons at the end of my commands.
Cheers!
也许您可以将
jQuery.fx.interval
设置为130(13是默认值)。那么每个动画都会类似控制台。Maybe you can just set
jQuery.fx.interval
to 130 (13 is default). Every animation would be console-like then.类似的东西: http://nayi.free.fr/error (不使用 jQuery)?
something like that : http://nayi.free.fr/error (not with jQuery) ?
不幸的是,基于一些研究博客 Ben Nadel 做到了,step 属性仅用于报告值而不是修改它。
但是,您可以非常简单地制定自己的方法来为该功能设置动画。您还可以查看 jQuery .animate() 源 并修改它可以满足您的需求,尽管我觉得使用像 setInterval 之类的东西和您自己的逻辑可能更容易。
如果 jQuery 允许您返回一个新的(更改的)值,我会很高兴,并且可能值得在他们的网站上作为功能更新提及。
Unfortunately, based on some research blogger Ben Nadel did, the step property is only used for reporting back the value and not modifying it.
However, you can make your own method for animating the feature pretty simply. You could also view the jQuery .animate() source and modify it to suite your needs, though I feel using something like a
setInterval
and your own logic may be easier.I would be nice if jQuery allowed you to return a new (altered) value though, and may be worth mentioning on their site as a feature update.