Animate a template in Meteor

时间:2015-12-14 18:07:48

标签: javascript animation meteor

There's a good library but I could use it to transition lists only. (I might be doing wrong ?)

However, I wanted to flip a counter whenever it was incremented. The solution I came with looks really ugly to me. I went from :

//counter.js
Template.counter.helpers({
    counter: function () {
        return Counts.get('counter');
    }
});

//counter.html
<template name="counter">
    <div class="ui clearing counterBlock">
        <h1 class="ui header centered">Contacts</h1>
        <h2 class="counter ui header centered">{{ counter }}</h2>
    </div>
</template>

for the non animated version to

//counter.js
var prevCounter = 0, counterTpl;

Template.counterBlock.onRendered(function () {
    counterTpl = Blaze.render(Template.counter, $(".counter")[0]);
});

Template.counter.helpers({
    counter: function () {
        var c = Counts.get('counter');

        if (c != prevCounter) {
            $(".counter .animated").removeClass('flipInX').addClass('flipOutX');
            Meteor.setTimeout(function () {
                prevCounter = c;
                Blaze.remove(counterTpl);
                counterTpl = Blaze.render(Template.counter, $(".counter")[0]);
            }, 1000);
        }

        return prevCounter;
    }
});

//counter.html
<template name="counterBlock">
    <div class="ui clearing counterBlock">
        <h1 class="ui header centered">Contacts</h1>
        <h2 class="counter ui header centered">
        </h2>
    </div>
</template>

<template name="counter">
    <span class="animated flipInX">{{counter}}</span>
</template>

I'm quite new to meteor universe but I'm still wondering if there's any proper way to animate a simple reactive change

1 个答案:

答案 0 :(得分:1)

Meteor确实提供了_uihook来动画删除或添加的DOM元素

Theres是您可以在Github

上找到的示例

以下是在domElement

中添加或删除元素时启用淡入/淡出动画的另一个示例
domElement._uihooks = {
    insertElement: function(node, next) {
      $(node)
        .hide()
        .insertBefore(next)
        .fadeIn();
    },
    removeElement: function(node) {
      $(node).fadeOut(function() {
        $(this).remove();
      });
    }
  }