【问题标题】:Animate an item rendered into a Marionette collection view动画呈现到 Marionette 集合视图中的项目
【发布时间】:2013-05-04 01:59:58
【问题描述】:

我有一个集合,其中包含代表应用导航项的模型。在我的应用程序中,导航位于屏幕的左侧,我希望能够在呈现导航项时一一滑动。导航容器是一个集合视图,导航项是它的项目视图。

在集合视图中,我已将 appendHtml 方法更改为如下所示。

    appendHtml: function (collectionView, itemView, index) {
            console.log('APPENDING ITEM VIEW', itemView.el);
            itemView.assignNewlyCreated();
            collectionView.$el.append(itemView.el);
            itemView.slideIn();
    },

项目视图具有以下相关方法:

    // Label the dom element as newly created
    assignNewlyCreated: function () {
        this.$el.addClass('newly-created');
    },

    // Slide in the item.
    slideIn: function () {
        console.log('sliding in element', this.el);
        this.$el.animate({left: 0});
    }

由于 newly-created 类的样式会将项目从屏幕上推到左侧,我在想如果我像这样附加它,然后在它进入 DOM 后将其滑入,它应该工作。不幸的是,这不起作用,导航项目已经出现在屏幕上,没有动画。我在这里做错了什么,有谁知道为什么这似乎不起作用?我认为我的应用程序的另一部分可能存在错误,但如果是这种情况并且如果已修复,上述代码是否可以工作?

【问题讨论】:

    标签: javascript marionette


    【解决方案1】:

    也许可以试试这样的:

    MyItemView = Backbone.Marionette.extend({
      template: "#template",
      className: "newly-created",
      onRender: function(){
        this.$el.animate({left: 0});
      }
    });
    

    这会直接在项目上设置类,然后在 CollectionView 呈现项目视图时滑动项目视图(onRender 会在定义时自动调用,您无需执行任何其他操作)。

    【讨论】:

    • 但最初当集合视图被渲染时,el 没有附加到 dom,所以他不会看到过渡效果。只有在稍后在集合上触发 add 事件时(即在附加集合之后查看到dom)他可以看到这个过渡
    猜你喜欢
    • 2012-03-15
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多