为多个元素创建同步动画
我试图在标题中描述得尽可能详细,但最终还是含糊其辞。因此,我将在这里详细说明:
我有以下 HTML:
<span class="item c1"><!--content--></span>
<span class="item c2"><!--content--></span>
<span class="item c3 c2"><!--content--></span>
<span class="item c1"><!--content--></span>
<span class="item c1 c4"><!--content--></span>
好的,所以假设的 的大小总是不同的,在本例中,这意味着元素的宽度也会不同。
因此,我需要做的是以某种方式按类别“过滤”内容。本质上,我需要让它们消失并在选择过滤器时重新出现。
我让它们消失的方法是将它们的宽度设置为 0px
并将不透明度设置为 0
- 这一切都是使用 jQuery 的 .animate()
过渡发生的>。
DOM 加载的那一刻,我将它们的“原始”宽度保存到 data-*
属性中:
jQuery(function(){
jQuery('.item').each(function(i, e) {
var $e = jQuery(e);
$e.data( 'origWidth', $e.css('width') );
});
});
现在,当它们再次显示(或者重新过滤,如果你愿意的话)时,它们都会获得相同的宽度(我知道为什么,但我不知道解决方法):
jQuery('.c'+id+'.item').stop().animate({
'width' : jQuery('.c'+id+'.item').data('origWidth'),
'opacity' : 1
});
//NOTE: the [id] in the above snippet is passed by a
//function and is the category id.
所以,我的“真正”问题是:有没有一种方法可以同步迭代 jQuery 数组并对属性进行动画处理 – 或者类似的方法。
谢谢你!
I tried to be as detailed as general in the title, but it ended up vague anyway. So, I will elaborate here:
I have the following HTML:
<span class="item c1"><!--content--></span>
<span class="item c2"><!--content--></span>
<span class="item c3 c2"><!--content--></span>
<span class="item c1"><!--content--></span>
<span class="item c1 c4"><!--content--></span>
Okay, so the size of the hypothetical <!--content-->
is always different, which - in this case - means that also the width of the elements will be different.
So, what I need to do is somehow "filter" the content by their classes. Essentially I need to make them disappear and re-appear when the filter is selected.
The way I make them disappear is to set their width to 0px
and the opacity to 0
—this all happens transitionally, using jQuery's .animate()
.
The moment the DOM loads, I save their "original" widths to a data-*
attribute:
jQuery(function(){
jQuery('.item').each(function(i, e) {
var $e = jQuery(e);
$e.data( 'origWidth', $e.css('width') );
});
});
Now, when they're being displayed again (or re-filtered, if you will) they all get the same width (I know why, but I don't know a way around it):
jQuery('.c'+id+'.item').stop().animate({
'width' : jQuery('.c'+id+'.item').data('origWidth'),
'opacity' : 1
});
//NOTE: the [id] in the above snippet is passed by a
//function and is the category id.
So, what my "real" questions is: Is there a way to synchronously iterate through a jQuery array and animate the attributes – or something along those lines.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,就几句话......您不必记住每个元素的宽度。 jQuery 的动画可以为您做到这一点。简单的例子: http://jsfiddle.net/AkJAm/3/
和js代码:
Ok, just a couple of words.. You do not have remember the width of each element. jQuery's animation can do it for you. Simple example: http://jsfiddle.net/AkJAm/3/
And js code:
首先
应该是
什么操作导致它们重新出现或被过滤?
我认为您错过了动画的持续时间。
此外
.item
似乎是多余的,因此:更新:
所以回到您的问题:
“那么,我的“真正的”问题是:有没有一种方法可以同步迭代 jQuery 数组并为属性设置动画 - 或者类似的东西。”
您需要迭代它们吗?
这不行吗?
那么使用
slideUp
和slideDown
或slideToggle
怎么样?First of all
should be
What action causes them to re-appear or being filtered?
I think you are missing the duration of the animation..
Also
.item
seems redundant, so:Update:
So back to your question:
"So, what my "real" questions is: Is there a way to synchronously iterate through a jQuery array and animate the attributes – or something along those lines."
Do you need to iterate through them?
Does this not work?
And what about using
slideUp
andslideDown
orslideToggle
?