在Sencha Touch 2中制作动画尺寸

时间:2012-03-26 13:09:46

标签: sencha-touch extjs sencha-touch-2

我正在尝试为数据视图的高度设置动画,但它目前只是围绕视口滑动面板而不是将其保持在原位并改变它的高度。代码如下:

Ext.Anim.run(el, 'slide', {
  from: { height: height },
  to: { height: newHeight },
  out: false,
  direction: 'up',
  easing: 'ease-out',
  duration: 1000
});

例如,height = 200,newHeight = 100将导致数据视图立即掉落,使其顶部位于视口下方200px处,然后动画回到视口顶部。

如何让它改变高度?感谢。

3 个答案:

答案 0 :(得分:9)

请尝试使用Ext.Animator.run

Ext.Animator.run({
    element: dataview.element,
    duration: 500,
    easing: 'ease-in',
    preserveEndState: true,
    from: {
        height: dataview.element.getHeight()
    },
    to: {
        height: 100
    }
});

在一个完整的例子中:

Ext.application({
    name: 'Sencha',

    launch: function() {
        var dataview = Ext.create('Ext.DataView', {
            fullscreen: true,
            style: 'background:red',
            store: {
                fields: ['text'],
                data: [
                    { text: 'one' },
                    { text: 'two' },
                    { text: 'three' }
                ]
            },
            itemTpl: '{text}'
        });

        Ext.Viewport.add({
            xtype: 'button',
            docked: 'top',
            handler: function() {
                Ext.Animator.run({
                    element: dataview.element,
                    duration: 500,
                    easing: 'ease-in',
                    preserveEndState: true,
                    to: {
                        height: 100
                    },
                    from: {
                        height: dataview.element.getHeight()
                    }
                });
            }
        });
    }
});

答案 1 :(得分:1)

由于我无法添加评论,因此我必须将此作为单独的答案。我只想添加到rdougan所说的内容并展示如何捕捉动画结束事件。我发现在上述情况下这是必要的,因为Sencha Touch的component.getTop / Left / Height / Width()函数在显示动画之后返回不正确的值。

dataview.setHeight(dataview.element.getHeight()); // you may or may not need this

console.log('height before\t', dataview.getHeight());

var a = new Ext.fx.Animation({
    element: dataview.element,
    duration: 500,
    easing: 'ease-in',
    preserveEndState: true,
    from: {
        height: dataview.element.getHeight()
    },
    to: {
        height: 100
    }
});

a.on('animationend', function (animation, element, isInterrupted) {
    console.log('height before\t', dataview.getHeight());
    dataview.setHeight(dataview.element.getHeight());
    console.log('height set\t', dataview.getHeight());
});

Ext.Animator.run(a);

我留下了一些记录,所以你可以看到我的意思。这个例子是针对ST 2.1 RC2编写的。

答案 2 :(得分:0)

这是一个干净的实用功能,可以用来完成这个

function animatron (target, prop, duration, to, from, easing) {
    // return if no target or prop
    if (target == null || prop == null) { return; }

    // defaults
    if (duration == null) { duration = 250; }
    if (to == null) { to = 0; }
    if (from == null) { from = target.getHeight(); }
    if (easing == null) { easing = 'ease-out'; }

    // to property
    var t = {};
    t[prop] = to;

    // from property
    var f = {};
    f[prop] = from;

    // Animation Options
    var opts = {
        duration: duration,
        easing: easing,
        element: target.element,
        from: f,
        preserveEndState: true,
        to: t
    };

    // Animation Object
    var anime = new Ext.fx.Animation(opts);

    // On animationend Event
    anime.on('animationend', function (animation, element, isInterrupted) {
        // Hide the target if the to is 0
        if (to == 0 && (prop == 'height' || prop == 'width')) {
            if (!isInterrupted) { target.hide(); }
        }

        // Update property if width or height
        if (prop == 'height') { target.setHeight(to); }
        if (prop == 'width') { target.setWidth(to); }

        // Dispatch 'animated' event to target
        target.fireEvent('animated', animation, element, to, from, isInterrupted);
    });

    // Show the target if it's hidden and to isn't 0
    if (target.getHidden() == true && to != 0) { target.show(); }

    // Run the animation
    Ext.Animator.run(anime);
}

您可以在目标元素上收听“动画”事件

animatron(dataview, 'height', 500, 0);
dataview.addListener('animated', function (animation, element, to, from, isInterrupted) {
    console.log('animation ended');
    console.log('interrupted: '+ isInterrupted);
});