2013-06-21 17 views
0

请帮我写动画中的匿名函数,我给出了我试过的代码,这里是DEMO,我想知道如何在动画中编写匿名函数,比如我在CSS按钮点击做到了,感谢提前在jQuery动画中编写匿名函数

<html> 
    <head> 
     <title></title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
     <style type="text/css"> 
      #example{width:100px;height:100px;background: #F00;} 
     </style> 
     <script> 
      $(document).ready(function(){ 
       $('#check').click(function(){ 
        $('#example').css({width:function(){ 
          console.log($(this).width()); 
          return $(this).width()=="0"? "100":"0"; 
        }}); 
       }); 
       $('#animate').click(function(){ 
        $('#example').animate({width:function(){ 
          console.log($(this).width()); 
          return $(this).width()=="0"? "100":"0"; 
        }}); 
       }); 
      }); 
     </script> 
    </head> 
    <body> 
     <div id="example">    

     </div> 
     <input type="button" value="Css" id="check"/> 
     <input type="button" value="Animate" id="animate"/> 
    </body> 
</html> 

回答

1

Demo

$('#animate').click(function() { 
    $('#example').animate({ 
     width: (function ($self) { 
      console.log($self.width()); 
      return $self.width() == "0" ? "100" : "0"; 
     })($('#example')) 
    }); 
}); 
+0

感谢了很多,斜面我们写的函数一样它在CSS? –

+0

不是因为对象没有作为param函数动画传递('this'指向窗口) –

+0

http://jsfiddle.net/72V8k/4/如何隐藏这个动态添加的按钮 –