2014-04-09 90 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> 

希望這有助於