2011-04-04 140 views
0

好的,我有這種形式。在表單中是一個輸入字段,當你鍵入一個?在輸入值的某處,我有一段運行onkeyup的代碼,它滑出一個元素(.searchEnter)以及更改一些css。jquery動畫問題

<script> 

       $(".askInput").keyup(function() { 

        if ($(this).val().indexOf("?") != -1) { 

         $('.searchEnter').animate({ 
         marginLeft: "310px" 
         }, 200); 
         $('.form1').get(0).setAttribute('action', 'post.php'); 


        } else { 

         $('.searchEnter').animate({ 
          marginLeft: "250px" 
          }, 200); 
         $('.form1').get(0).setAttribute('action', 'search.php'); 


        } 



       }); 

       </script> 

當我在輸入中只輸入一個問號時,.searchEnter立即滑出。然而,問號前面的字符越多(即「你叫什麼名字?(17個字符之前?)」),則.animate踢入並滑動所需的時間越長.search輸出。爲什麼會發生這種情況,我該如何解決?

回答

0

嘗試$('。searchEnter')。stop()。animate ...

+0

非常感謝它的工作。 – hubrid 2011-04-04 04:38:23

0

它排隊所有的動畫。在致電animate之前致電stop

因此,不是這樣的:

$(".searchEnter").animate(... 

這樣做:

$(".searchEnter").stop().animate(... 

另外,我注意到你正在使用get(0).setAttribute(...。爲什麼不使用attr

+0

哇,這個問題和答案論壇的岩石。謝謝你所有的建議幫助! – hubrid 2011-04-04 04:37:44