[英]when element is removed, then reposition/animate each next element jQuery
Actually I have dynamic notification ".noti", and all has 实际上,我有动态通知“ .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});
Now what I want to do is that, when I removed a element, then each next element must reposition/adjust their "right:xxx" property. 现在我要做的是,当我删除一个元素时,每个下一个元素必须重新定位/调整其“ right:xxx”属性。
for example I have 5 div : 例如我有5 div:
|_ _ _ _ _|
and I removed the right most div 我删除了最右边的div
|_ _ _ _ |
then all other divs must reposition as 那么所有其他div必须重新定位为
| _ _ _ _|
I tried ".next": 我尝试了“ .next”:
var next = $(this).closest('.noti').next('.noti');
if(next.length > 0){
var nextright = next.css('right');
nextright = nextright.split('px').join('');
next.animate({right:nextright-250},300);
}
but it'll only reposition the next div. 但只会重新定位下一个div。
So can somebody tell me that how can I use ".each()" or ".nextAll()" or a "for loop" to reposition all next elements. 因此有人可以告诉我,如何使用“ .each()”或“ .nextAll()”或“ for循环”来重新放置所有下一个元素。
Thanks & regards 感谢和问候
UPDATE: 更新:
Now I tried : 现在我尝试了:
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});
}
});
}
Its works good only when I want to remove the right most element. 仅当我想删除最合适的元素时,它的效果很好。 but if I removed any element from the middle, than each ".noti" reposition their "right" property. 但是,如果我从中间删除了任何元素,则每个“ .noti”都会重新定位其“ right”属性。 But I want that only the "nextAll" elements must reposition. 但我希望只有“ nextAll”元素必须重新定位。
UPDATE : SOLUTION 更新:解决方案
Finally this problem is solved {solution is based on Tandon's answer} http://jsfiddle.net/7f2aU/5/ 终于解决了这个问题{解决方案基于Tandon的答案} http://jsfiddle.net/7f2aU/5/
Thanks 谢谢
div
to remove it DEMO 单击div
将其删除 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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.