40

Meteor 如何处理实时变化?例如,我不希望更改是瞬时的,而是带有某种动画。如果我们使用 css 动画/过渡放置正在更改的项目,这是否有效?旧浏览器的 jQuery 动画怎么样?

4

3 回答 3

12

这是一个使用流星的简单动画的工作示例。

这里的情况是我们有一个项目列表。如果用户单击这些项目中的任何一个,该项目将向左移动 20 像素。

JS

//myItem
Template.myItem.rendered = function(){
  var instance = this;
  if(Session.get("selected_item") === this.data._id){
    Meteor.defer(function() {  
      $(instance.firstNode).addClass("selected"); //use "instance" instead of "this"
    });
  }
};

Template.myItem.events({
  "click .myItem": function(evt, template){
    Session.set("selected_item", this._id);
  }
});


//myItemList
Template.myItemList.helpers({
  items: function(){
    return Items.find();
  }
});

模板

<template name="myItem">
  <div class="myItem">{{name}}</div>
</template>

<template name="myItemList">
  {{#each items}}
    {{> myItem}}
  {{/each}}
</template>

CSS

.myItem { transition: all 200ms 0ms ease-in; }
.selected { left: -20px; }

除了使用花哨的 CSS,您还可以使用 jQuery 进行动画处理:

Template.myItem.rendered = function(){
  if(Session.get("selected_item") === this.data._id){
    $(this.firstNode).animate({
      left: "-20px"
    }, 300);
  }
};

但随后您需要删除 CSS 代码。

.myItem { transition: all 200ms 0ms ease-in; }
.selected { left: -20px; }

于 2013-07-18T12:15:45.320 回答
5

有这样的解决方法:

<template name="foo">
  ..content..
  {{bar}}
</template>

在这种情况下,Meteor 将在Template.foo.bar每次渲染此模板时调用。所以在这个函数中你可以做各种 Jquery 或 CSS3 动画(例如通过向模板 div 添加一个类)。

于 2012-06-04T10:18:12.570 回答
2

对于 CSS 过渡,您可以使用两个选项:

1. Reactively: the Meteor way
2. Directly: the jQuery way

这是一个工作示例: http ://bindle.me/blog/index.php/658/animations-in-meteor-state-of-the-game

于 2013-11-17T07:46:09.183 回答