简体   繁体   English

对div元素进行动画处理,更改其在div列表中的位置

[英]Animate a div element changing its position in a div list

I'm trying to animate a div element as it repositions itself in a sorted div list, so it doesn't make an instant jump, but a smooth transition. 我正在尝试对div元素进行动画处理,因为它会将自己重新定位在已排序的div列表中,因此它不会立即跳转,而是平滑过渡。 The list data comes from a Mongo collection and every document yields a template (a div element). 列表数据来自Mongo集合,每个文档都产生一个模板(一个div元素)。 Every document has a 'votes' property, based on which the list is sorted. 每个文档都有一个“投票”属性,根据该属性对列表进行排序。 The logged-in user can vote once for every post, and if a post reaches a higher number of upvotes it gets repositioned. 登录的用户可以为每个帖子投票一次,并且如果帖子达到更高的投票数,它将被重新定位。

I'm following the Discover Meteor book, in case anyone is familiar with it. 如果有人熟悉它,我会关注“发现流星”一书。 This was handled by setting a CSS transition property: transition: all 300ms 0ms ease-in; 这是通过设置CSS过渡属性来处理的: transition: all 300ms 0ms ease-in; , re-rendering the post in the old spot, then changing the relative position of the post once it's re-rendered. ,在旧位置重新渲染帖子,然后在重新渲染后更改帖子的相对位置。 The latter was, of course, handled in the onRendered function. 后者当然是在onRendered函数中处理的。

This is where the trouble starts. 这是麻烦开始的地方。 Apparently, onRendered doesn't fire once the div element receives enough votes to change its position. 显然,一旦div元素获得足够的投票来更改其位置,onRendered便不会触发。

So, my question would be: how do I animate the element changing its position, once the list gets re-ordered? 因此,我的问题是:一旦列表重新排序,如何为元素更改位置动画?

EDIT: Here's the code: 编辑:这是代码:

Template.postItem.onRendered(function(){
    //animate post from previous position to new position
    console.log(/*something so I know the onRendered function is firing*/);
    var instance = this;
    var rank = instance.data._rank;

    var $this = $(this.firstNode);
    var postHeight = 80;
    var newPosition = rank * postHeight;

    //if element has currentPosition (i.e. is not first ever rendered)
    if(typeof(instance.currentPosition)!=='undefined'){
        var previousPosition = instance.currentPosition;
        //calculate difference between old position and new position and send
        //element there
        var delta = previousPosition - newPosition;
        $this.css("top", delta + "px");
    }

    //let it draw in the old position, then...
    Meteor.defer(function(){
        instance.currentPosition = newPosition;
        //bring element back to its new original position
        $this.css("top", "0px");
    });
});

I recommend to use Blaze's _uihooks . 我建议使用Blaze的_uihooks You could register a hook which will be called when Blaze intends to move the respective DOM element. 您可以注册一个挂钩,当Blaze打算移动相应的DOM元素时,该挂钩将被调用。

For example: 例如:

var hook = {
    moveElement: function(node, next) {
        var $node = $(node);
        /* Add animation code. */
        $node.animate();
    }
}

Template.postItem.onRendered(function() {
    /* Set hook to first node. */
    this.firstNode._uihooks = hook;
    /* Or to any other DOM element. */
    this.find('.postItem')._uihooks = hook;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM