感覺這樣寫有點(diǎn)煩,有更簡潔的辦法沒?比如用判斷語句?
<!DOCTYPE html>
<html>
??? <head>
??????? <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
??????? <title>jQuery動畫特效</title>
??????? <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
??????? <style >
??????????? div{
??????????????? position:absolute;
??????????????? top:200px;
??????????????? width:50px;
??????????????? height:50px;
??????????????? background-color:red;
??????????????? color:white;
??????????????? border-radius:20px;
??????????????? text-align:center;
??????????????? line-height:20px;
??????????????? left:50px;
??????????????? }
??????? </style>
??
??? <body>
??????? <input id="left" type="button" value="左移" />
??????? <input id="right" type="button" value="右移" />
??????? <div><font></font></div>
??????? <script>
??????????? $(function(){
??????????? var $timel=0;
??????????? var $timer=0;
??????????? $("#left").bind("click",function(){???????????????? ++$timel;
??????????????? $("div").animate({
??????????????????? left:"+=50px"
??????????????? },3000,function(){
??????????????????? $(this).animate({
??????????????????????? width:'+=20px',
??????????????????????? height:'+=20px'
??????????????????? },2000,function(){
??????????????????????? $("font").html("第"+$timel+"次左移");
??????????????????? });
??????????????? });
???????????? });
??????????????? $("#right").bind("click",function(){
??????????????????? ++$timer;
??????????????????? $("div").animate({
??????????????????????? left:"-=50px"
??????????????????? },3000,function(){
??????????????????????? $(this).animate({
??????????????????????????? width:'-=20px',
??????????????????????????? height:'-=20px'
??????????????????????? },2000,function(){
??????????????????????????? $("font").html("第"+$timer+"次右移");
??????????????????????? });
??????????????????? });
??????????????? });
??????????? });
??????? </script>
??? </body>
</html>
2016-05-04
我用了一個function來包裹變形的動作 代碼如下
2016-05-04
?<script>
? ? ? ? ? ? $(function(){
? ? ? ? ? ? var $timel=0;
? ? ? ? ? ? var $timer=0;
? ? ? ? ? ? $("#left").bind("click",function(){ ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? go($timel,"-=50","-=20")
? ? ? ? ? ? ?});
? ? ? ? ? ? ? ? $("#right").bind("click",function(){
? ? ? ? ? ? ? ? ? ? go($timer,"+=50","+=20")
? ? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? ? ? function go(time,dis,wh){
? ? ? ? ? ? ? ? ++time;
? ? ? ? ? ? ? ? $("div").animate({
? ? ? ? ? ? ? ? ? ? left:dis+"px"
? ? ? ? ? ? ? ? },3000,function(){
? ? ? ? ? ? ? ? ? ? $(this).animate({
? ? ? ? ? ? ? ? ? ? ? ? width:wh+"px",
? ? ? ? ? ? ? ? ? ? ? ? height:wh+"px"
? ? ? ? ? ? ? ? ? ? },2000,function(){
? ? ? ? ? ? ? ? ? ? ? ? $("font").html("第"+time+"次右移");
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? </script>
2016-05-04
可以把兩個bind合并到一起啊。?$("#right").bind().bind();這樣會稍微簡單點(diǎn)吧