每个类元素的简单jquery .hover()方法

没有做太多的jquery并遇到了问题。 我想用类.social-tile绑定所有div的hover事件。 我是这样做的:

$(function(){ var social_default = $('.social-tile').css('margin-right'); $('.social-tile').each(function(){ $(this).hover(function(){ $(this).animate({ 'margin-right': '0px' },500); },function(){ $(this).animate({ 'margin-right': social_default },500); }); }); }); 

当我将鼠标hover在一个元素上时,会触发所有div的动画,并且它们一次全部移动。 我想只为我hover的特定元素制作动画。

可能这里有一个非常简单的解决方法,谢谢。

 $(function() { $('.social-tile').hover( function() { $(this).data('prev-margin-right', $(this).css('margin-right') ); $(this).animate({ 'margin-right': '0px' }, 500); }, function() { $(this).animate({ 'margin-right': $(this).data('prev-margin-right') }, 500); }); }); 

试试这个,抱歉 – 在这里工作演示: http : //jsfiddle.net/8vFAD/1/

 $('.social-tile').live('mouseover', function(){ $(this).animate({ 'margin-left': '15px'} ,500).animate({ 'margin-left': '30px'} ,500); }); 

好吧你的变量social_default,其中包含.socialtile,它将调用分配给它的所有div,将其更改为(this)。