Javascript 删除元素后,请重新定位/设置下一个元素jQuery的动画
实际上,我有动态通知“.noti”,所有的都有Javascript 删除元素后,请重新定位/设置下一个元素jQuery的动画,javascript,jquery,html,css,Javascript,Jquery,Html,Css,实际上,我有动态通知“.noti”,所有的都有 position:fixed; bottom:0; width:250px; right://adjusted via jQuery bcz they are dynamically added var s = $('body').find('.noti').size(); xxxxx.css({right:s*250}); 现在我想做的是,当我删除一个元素时,下一个元素必须重新定位/调整它们的“right:xxx”属性 例如,我有
position:fixed;
bottom:0;
width:250px;
right://adjusted via jQuery bcz they are dynamically added
var s = $('body').find('.noti').size();
xxxxx.css({right:s*250});
现在我想做的是,当我删除一个元素时,下一个元素必须重新定位/调整它们的“right:xxx”属性
例如,我有5个div:
|_ _ _ _ _|
我删除了最右边的div
|_ _ _ _ |
然后,所有其他div必须重新定位为
| _ _ _ _|
我尝试了“.next”:
但它只会重新定位下一个分区
有人能告诉我如何使用“.each()”或“.nextAll()”或“for循环”来重新定位所有下一个元素吗
谢谢和问候
更新:
现在我试着:
var parent = $(this).closest('.noti'),next = parent.next('.noti');
var df = parent.attr('data-for');
if(next.length > 0){
$('.noti').each(function(){
if(df != $(this).attr('data-for')){
var t=$(this),pos = t.css('right');
pos = pos.split('px').join('');
t.animate({right:pos-250});
}
});
}
只有当我想删除最右边的元素时,它才有效。但如果我从中间删除任何元素,则每个“.noti”都会重新定位其“right”属性。但我希望只有“nextAll”元素必须重新定位
更新:解决方案
最后,这个问题得到了解决{解决方案基于Tandon的答案}
谢谢单击div
将其删除
谢谢你的回复。但是亲爱的,我已经提到这个元素有“position:fixed;”css属性和jQ调整后的“right:xxx”属性。@NikitaChaudhary抱歉,我没有注意到它。“我正在考虑另一个解决方案。”NikitaChaudhary还补充道。随便问你不懂的问题:)它很管用,但不适合我的情况。bcz当点击“.noti>.title>.close”时,“click”函数触发,“.noti”本身是一个父div,并直接附加到主体{我在问题的第一行中已经提到了这一点}。那么,您可以根据这些条件修改您的解决方案吗。我也在尝试这样做。无论如何,谢谢你的帮助。请检查更新。感谢您的迟到回复。这是固定小提琴-->
var parent = $(this).closest('.noti'),next = parent.next('.noti');
var df = parent.attr('data-for');
if(next.length > 0){
$('.noti').each(function(){
if(df != $(this).attr('data-for')){
var t=$(this),pos = t.css('right');
pos = pos.split('px').join('');
t.animate({right:pos-250});
}
});
}
var width = $('#container div').width();
$('#container div').each(function(index){
// set css property `right` to the width * index (zero-based) + 30 (random number)
$(this).css("right", ((width * index) + 30));
});
$("#container div").click(function(){
var thisIndex = $(this).index(); // current element's index
// console.log(thisIndex); a check
$(this).siblings("div").each(function(index){
// to make sure the element `$(this)` (the sibling) is to the left of the element clicked on
if((index + 1) > thisIndex){
var css = parseInt($(this).css("right"), 10); // parse the property
$(this).css("right", css - width); // set it shifted to right by an offset equal to width
// console.log($(this).css("right")); a check
}
});
$(this).remove(); // finally, remove it
});