2013-08-22 126 views
0

我有这段代码,它将类“块”中的每个元素移动到左侧10个像素。我想要它删除剩下的300个像素的任何元素。 $(this).remove()为什么不起作用,我该如何解决这个问题?删除类中的特定元素

$(".block").animate({left:"-=10"},speed,"linear",function(){ 

     if(parseInt(this.style.left) < 300) 
     { 
      $(this).remove(); 
      //something 
     }else{ 
      //something 
     } 
    }); 

HTML:

<div id="container"> 
     <span class="block"></span> 
     <span class="block"></span> 
    </div> 

这里是我的所有代码http://jsbin.com/ExET/1/

+3

工作对我来说:http://jsfiddle.net/cmvUZ/ –

+0

@JosephSilber对我来说,元素有类“块”,它们不在类“块”内 –

+0

这应该没有什么区别。我只是用它来显示东西在移动。你可以发布你的代码不工作的小提琴吗?请务必提供与此问题相关的代码:[** SSCCE **](http://sscce.org/)。 –

回答

1

喜欢这个? jsFiddle

$('div').on('mouseover', function() { 
    $(this).animate({ 
     left: '+=10' 
    }, 200, 'linear', function() { 
     if($(this).offset().left > 50) { 
      $(this).remove(); 
     } else { 
      $(this).css('background', 'blue'); 
     } 
    }); 
}); 

您需要更改的值,但它达到你想要的效果。