问题
Please help me to write the anonymous function in animation, I have given the code which I have tried, Here is the DEMO, I want to know how to write the anonymous function in animation like what I did in the Css button click, thank in advance
<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'))
});
});
来源:https://stackoverflow.com/questions/17232179/writing-anonymous-function-in-jquery-animation