开发者

jQuery bind unbind animation

I made this piece of code,i'm trying to animate a block of text/div back and forward, but why this works only the first time for the "placeRight" function? Is there something wrong with the right : "+=750" attribute?

$(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").cl开发者_Go百科ick(placeRight);
    }


You could do it with less code. Here's a working demo: http://jsfiddle.net/kkZtD/1/


Yeah, you have double time $("#text").animate( { left : "+=750" }, 1300);
so you're trying to place it always to +750px position

change it like this

$(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);
}


try this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜