javascript - jQuery 动画混淆行为

标签 javascript jquery jquery-ui jquery-animate

诚然,我是 jQuery 的新手,但我知道一点 javascript,我正在尝试使用几个动画为 div 设置动画。

我想让div先移到页面中央 然后我想让它在到达中心后翻转然后展开。

我遇到的问题是使用 .animate() 方法更改 div 的 topleft 属性以获取它到中心。


问题是 moveToCenter 函数中的 .animate 方法实际上并没有为移动到中心设置动画,它只是跳过动画部分(属性变化,但变化不是动画)并继续进行翻转和休息

谁能告诉我为什么会这样/我哪里出错了?

如何解决(如果有的话)?


HTML

<div id="flip" style="background-color:#ff0000;
               width:200px;
               height:200px;
               position:absolute">

  <h4> printf("Hello World"); </h4>
</div>

JS

function moveToCenter()
{
    $('#flip').animate({'top':'300','left':'300'});
}

$(document).ready(function() {
    $('#flip').click( function() {

            moveToCenter(); //function that is supposed to moves the div to the center

            $(this).flip({
                    content: 'Scanf()',
                    color: '#00FF00',
                    direction: 'lr',
                    onEnd:  function(){ $('#flip').animate({ 
                    'height' : '400', 'width': '300'
                    }       
                    ,'500','swing');

                    lol(); //Another function that does some other animations
                    }
                });
            });
        });

最佳答案

如果你想在动画完成后翻转,添加这样的回调:

function moveToCenter(done){
    $('#flip').animate({'top':'300','left':'300'}, done);
}

该函数将在动画完成时调用done 方法。所以现在你只需要传递它:

moveToCenter(function() {
    // now the animation is done, move on
    $(this).flip({ //... etc

关于javascript - jQuery 动画混淆行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13584333/

相关文章:

javascript - 如何在暂停后进行 Javascript 函数调用

javascript - 如何跨域获取iframe contentWindow高度

javascript - 使 CSS 通用以用于图像居中

javascript - 将动态变化的信息添加到 ng-binding 中

javascript - 使用 Jquery 淡入和淡出我的文本,还显示 :none

javascript - jquery悬停li以显示段落内?

javascript - Jquery:小部件的初始功能

javascript - 如何访问元素的 cssText?

javascript - 禁用在 jQuery DateTimePicker 中的下一个/上一个操作期间自动更改日期值

php - 如何通过 AJAX 传递值来更改 SQL 搜索查询并将结果返回为 HTML?