简体   繁体   English

动画后jQuery文本更改

[英]JQuery text change after animations

I'm trying to animate a div box for several times when a button is pressed. 按下按钮时,我试图为div框设置动画多次。 It shall go right, and again right, then it comes a bit down and the text inside should change, and then it shall go left, and again left to its original place. 它应该向右移动,然后再次向右移动,然后向下移动一点,里面的文本应该更改,然后向左移动,然后再次左移到其原始位置。 There are two problems that I need help with: 我需要解决两个问题:

  1. The text change doesn't occur when it should! 文本更改不会在应有的时候发生! It changes immediately after I press the button! 按下按钮后,它立即改变! What should I do to run the text change just AFTER the third animation? 我应该怎么做才能在第三个动画之后运行文本更改?

  2. I get the value of an input number box, and want to apply it as the speed argument. 我得到一个输入数字框的值,并想将其用作速度参数。 But when I put number instead of speed , it does not work. 但是当我用number代替speed ,它不起作用。 I have to set it manually like 2000. What is wrong here? 我必须手动将其设置为2000。这里出了什么问题?

JavaSript Code: JavaSript代码:

$(document).ready(function(){
  $("button").click(function(){
  var d=$("#t");
  var number=$("#number1").val();
  var speed=2000;

  if(state==true){
        d.animate({left:'+=230px'}, speed);
        d.animate({left:'+=230px'}, speed);
        d.animate({top:'+=20px', backgroundColor: "#f09090", text:'12'}, speed/4);
        $('#span').fadeOut(500, function() {
        $(this).text('a1').fadeIn(500);
        });     
        d.animate({left:'-=230px'}, speed);
        d.animate({left:'-=230px'}, speed);
        d.fadeOut();

        }
  });
});

You can also try this: use .when and .then 您也可以尝试:使用.when和.then

Here you go: http://jsfiddle.net/9RuTX/1/ 在这里,您可以: http : //jsfiddle.net/9RuTX/1/

In terms of your #2 problem. 关于您的第二个问题。 I tested it out and it seems to work fine. 我测试了一下,它似乎工作正常。 Have you consoled it out and see if its getting in your var number. 您是否进行过调配,并查看它是否进入了您的var号。 Perhaps do a typeof on it and see what it is. 也许对它进行typeof看看它是什么。

$(document).ready(function(){
  $("button").click(function(){
  var d=$("#t");
  var number=$("#number1").val();
  var speed=2000;
  if(state==true){
        $.when(
        d.animate({left:'+=230px'}, speed),
        d.animate({left:'+=230px'}, speed),
        d.animate({top:'+=20px', backgroundColor: "#f09090", text:'12'}, speed/4)
       ).then( function(){
            $('#span').fadeOut(500, function() {
                  $('#span').hide().text('a1').fadeIn(500);
            })
          }
             ).then( function(){
        d.animate({left:'-=230px'}, speed)
        d.animate({left:'-=230px'}, speed)
        d.fadeOut()
        }
       )
  }
  });
});

This happens because jQuery enqueue's animations per-element, and the span is a different element. 发生这种情况是因为jQuery将每个元素的动画排入队列,并且span是一个不同的元素。 You should put the text changing code in a callback function for when the previous animation completes. 您应该在上一个动画完成时将文本更改代码放入回调函数中。 Here's an example. 这是一个例子。

<script> 
$(document).ready(function(){
  $("button").click(function(){
  var d=$("#t");
  var number=$("#number1").val();
  var speed=2000;

  if(state==true){
        d.animate({left:'+=230px'}, speed);
        d.animate({left:'+=230px'}, speed);
        d.animate({top:'+=20px', backgroundColor: "#f09090", text:'12'}, speed/4, "swing", function(){
            $('#span').fadeOut(500, function() {
                $(this).text('a1').fadeIn(500);
            });
        });
        d.animate({left:'-=230px'}, speed);
        d.animate({left:'-=230px'}, speed);
        d.fadeOut();

        }
  });
});
</script>

If needed, you can also delay you following animation so that it will not run simultaneously with the text changing code like this. 如果需要,您还可以延迟跟随动画,以使其不会与像这样的文本更改代码同时运行。

d.delay(500).animate({left:'-=230px'}, speed);

Here is a working example based on you jsFiddle. 这是一个基于您的jsFiddle的工作示例。

http://jsfiddle.net/LSegC/ http://jsfiddle.net/LSegC/

Try this 尝试这个

    d.animate({left:'+=230px'}, speed)
     .animate({left:'+=230px'}, speed)
     .animate({top:'+=20px', backgroundColor: "#f09090", text:'12'}, speed/4)
     .queue(function(){
         var q = $(this);
         $('#span').fadeOut(500, function() {
             $(this).text('a1').fadeIn(500, function(){ q.dequeue() });
         });
     })
     .animate({left:'-=230px'}, speed)
     .animate({left:'-=230px'}, speed)
     .fadeOut();

Just add it to the animation queue and it will get executed after the previous animations are done. 只需将其添加到动画队列中,它将在之前的动画完成后执行。

Related demo: http://jsfiddle.net/DerekL/qn893/1/ 相关演示: http : //jsfiddle.net/DerekL/qn893/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM