简体   繁体   English

下一个动画(这个)下所有使用jQuery的父母?

[英]Animate (this) nextAll parents with jquery?

I am trying to animate all of the parents of an element after the one clicked. 单击某个元素后,我正在尝试为该元素的所有父项设置动画。

I have tried several iterations of the following: 我尝试了以下几种迭代:

$(this).parent().nextAll().animate({
  marginTop: '-=50px'
}, 500);

But I don't think I am getting the syntax right... 但是我不认为语法正确...

The parent element has a class of ".useritem" and I have a feeling I have to use it but I am not sure where as I thought specifying that the parent>of this item>all of them after>move up 50 px, would work. 父元素有一个“ .useritem”类,我有一种必须使用的感觉,但是我不确定在我认为指定父项>此项目>所有之后>向上移动50像素的地方工作。

Any help, guidance or advice would go a long way, thank you. 任何帮助,指导或建议都会走很长一段路,谢谢。

Edit 1: HTML 编辑1:HTML

My HTML is created dynamically based on the server response. 我的HTML是根据服务器响应动态创建的。 It will look like this however: 它看起来像这样:

<div id='useritem' class='useritem'>
    <div id='msgnotification' class='msgnotification'>0</div>
    <img id='userimg' class='userimg' src='data/here'>
    <div id='usertxt' class='usertxt'>
        <div id='name' class='name'>User</div>
        <div id='username' class='username'></div>
    </div>
    <div id='useradd' class='useradd'><i class='fa fa-user-plus'></i></div>
</div>

There are multiples of these stacked and when a user clicks on 'useradd' it's width is animated to 0 and the marginTop code should fire on every 'useritem' under it moving them up. 这些堆叠有很多倍,当用户单击“ useradd”时,其宽度设置为0,并且marginTop代码应在其下的每个“ useritem”上触发。

Thank you for your input and thank you to @Barmar for pointing out that my code /should/ have worked, I went over it again and realised where I was going wrong. 谢谢您的输入,也感谢@Barmar指出我的代码/应该/已经工作了,我再次进行了遍历,意识到我出了问题。 I had wrapped the marginTop code in a timeout to wait for the initial clicked parent element to have a set animated width of 0. But of course when you try to call 'this' again, the element isn't there so the code didn't have an initial selector. 我已经将marginTop代码包装到一个超时中,以等待初始单击的父元素的动画宽度设置为0。但是,当您再次尝试调用“ this”时,该元素不在那里,因此代码没有没有初始选择器。

I fixed this by using .delay() instead. 我改用.delay()修复了这个问题。 And now my initial code is working just without the timeout! 现在,我的初始代码可以正常工作了!

Thank you again for your input and time. 再次感谢您的投入和时间。 This is the updated code: 这是更新的代码:

$(this).parent().animate({
    'width': 0,
    'padding-left': 0,
    'padding-right': 0
}, 300, function() {
    $(this).next().animate({
        marginTop: '-=61px'
    }, 300);
});

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

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