2014-04-09 143 views
0

我想动画我点击一个按钮。它有类.testclass。我有一个以上的名称的按钮,这就是为什么我需要指定正确的。为什么“这个”在这里不起作用?jQuery点击并动画这

问题是我需要在函数中调用“this”,因为我想要一个动画循环。

的CoffeeScript:

$('.testclass').click -> 
     colorFader = -> 
      $(this).animate 
       backgroundColor: '#33e27d', 1000, 'linear', -> 
        $(this).animate 
         backgroundColor: '#27ae60', 1000, 'linear', colorFader 
     colorFader() 

OK,在JavaScript就应该是这样的:

$('.testclass').click(function() { 
    var colorFader; 
    colorFader = function() { 
    return $(this).animate({ 
     backgroundColor: '#33e27d' 
    }, 1000, 'linear', function() { 
     return $(this).animate({ 
     backgroundColor: '#27ae60' 
     }, 1000, 'linear', colorFader); 
    }); 
    }; 
    return colorFader(); 
}); 

回答

0

确定。我有答案。

这里是正确的代码如何将这个添加到函数中。

$('.testclass').click -> 
     colorthis = $(this) 
     colorFader = -> 
      colorthis.animate 
       backgroundColor: '#33e27d', 1000, 'linear', -> 
        colorthis.animate 
         backgroundColor: '#27ae60', 1000, 'linear', colorFader 
     colorFader() 
0

为什么你已经在动画动画? 这通常是我如何动画:

jQuery(".one_of_my_buttons").on("click",function() 
{ 
     jQuery(this).animate({left: "100px"}, 500, function() { 
     // Animation complete. 
     }); 
}); 
+0

什么OP是使用被称为CoffeeScript的 –

0
$(document).ready(function(){ 
    $(".classname").click(function() { 
    $(this).animate({{letterSpacing:"+=10px"}}, 1000); 
    }); 
}); 
+0

HM确定。那不是问题。但我需要调用一个函数,就像你在我的例子中看到的那样。任何想法? – steay

+0

请通过解释此代码的作用以及它如何回答问题来改进此答案。它处于低质量帖子审核队列中,并且很有可能以当前形式被删除。 –

0

是可以调用click事件里面的函数如下

$(document).ready(function(){ 
    $(".classname").click(function() { 
    $(this).animate({letterSpacing:"+=10px"}, 1000); 
    anyfunctionname(); 
    }); 
}); 
+0

请看看我的例子。我已经添加了一个JavaScript版本。我需要在一个函数中调用“this”。如你看到的。你误解了我 – steay

+0

你想动画你点击的同一个按钮,我是对吧? – ManoharSingh

+0

是的。我想在一个函数中为它制作动画。此函数重复colorfade.You可以在我的JavaScript示例中看到它。当我与班级通话时,它的工作。但是那个类的所有按钮都是动画的。但我只想让这个按钮变成动画,我点击了它。现在希望它清楚。 :-) – steay

0

您可以按键值传递给函数和孤独,那么你可以选择动画..按钮

<input type="button" onclick="colorfadar(this.value)" class="one" Value="Save"> 
<input type="button" onclick="colorfadar(this.value)" class="one" Value="Delete"> 

<script type="text/javascript"> 
     var cols1 = "#ffcc00,#eeeeee,#3b5998".split(","); 
     var cols = "500,200,300".split(","); 
     var cPos = 0; 

    function colorfadar(str) { 
     $('input[value="'+str+'"]').animate({backgroundColor:cols1[cPos],width:cols[cPos]},1000); 
     cPos++ 
     if (cPos == cols.length) { 
      cPos = 0 
     } 
     window.setTimeout(function() { colorfadar(str) }, 500) 
    } 

</script> 

希望这有助于