jQuery绑定unbind动画

我制作了这段代码,我试图为一块文本/ div前后动画制作动画,但为什么这只是第一次为“placeRight”函数工作? right : "+=750"有什么问题right : "+=750"属性?

 $(document).ready( function ( ) { $("#text").click(placeRight); }); var placeRight = function() { $("#text").animate( { right : "+=750" }, 1300); $("#text").unbind("click"); $("#text").click(placeLeft); } var placeLeft = function() { $("#text").animate( { left : "+=750" }, 1300); $("#text").unbind("click"); $("#text").click(placeRight); } 

是的,你有两倍的时间$("#text").animate( { left : "+=750" }, 1300);
所以你试图把它总是放在+ 750px位置

像这样改变它

 $(document).ready( function ( ) { $("#text").click(placeRight); }); var placeRight = function() { $("#text").animate( { right : "+=750" }, 1300); $("#text").unbind("click"); $("#text").click(placeLeft); } var placeLeft = function() { $("#text").animate( { left : "-=750" }, 1300); //or { right: 0 } $("#text").unbind("click"); $("#text").click(placeRight); } 

你可以用更少的代码来做到这一点。 这是一个有效的演示: http : //jsfiddle.net/kkZtD/1/

试试这个:

 $(document).ready(function(){ $("#text").click(function(){ $(this).animate({ right: "+=750" }, 1300).animate({ left: "0" }, 1300); }); });