Backbone Marionette - Add a visual effect when switching view

后端 未结 5 542
半阙折子戏
半阙折子戏 2020-12-23 18:16

Is there a convenient way to add an effect when I leave a page (close a view/layout) and open a new one in the same region ? (something like a fade effect)

相关标签:
5条回答
  • 2020-12-23 18:30

    This seems to work for me:

    this.views = {
        messageItem: Marionette.ItemView.extend({
            template: Handlebars.templates.messaging_item,
            tagName: "li",
            className: "messaging-item",
            render: function(){
                this.$el.html(this.template(this.model.attributes));
                this.$el.hide();
            },
            onShow: function(){
                this.$el.slideDown(800);
            }
        })
    };
    
    0 讨论(0)
  • 2020-12-23 18:34

    You could override the close function on the view, doing something like this:

    close: function () {
      // fancy fade-out effects
      Backbone.Marionette.View.prototype.close.apply(this, arguments);
    }
    

    And do something similar with your render functions.

    0 讨论(0)
  • 2020-12-23 18:38

    Marionette regions have a method called open that by default just replace the HTML of the old view with the new view. You can override this method to do any animation you like. From the region documentation:

    Set How View's el Is Attached

    If you need to change how the view is attached to the DOM when showing a view via a region, override the open method of the region. This method receives one parameter - the view to show.

    The default implementation of open is:

    Marionette.Region.prototype.open = function(view){
      this.$el.html(view.el);
    }
    

    This will replace the contents of the region with the view's el / content. You can change to this be anything you wish, though, facilitating transition effects and more.

    Marionette.Region.prototype.open = function(view){
      this.$el.hide();
      this.$el.html(view.el);
      this.$el.slideDown("fast");
    }
    

    This example will cause a view to slide down from the top of the region, instead of just appearing in place.

    0 讨论(0)
  • 2020-12-23 18:49

    I think this could be useful for you.

    The following marionette plugin that adds 4 kind of transitions. There can be easily added more transition types.

    Basically instead of using yourRegion.show(view)... you can use now yourRegion.showAnimated(view, {animationType: 'yourAnimation'});

    it's very easy to use.

    https://github.com/marcinkrysiak1979/marionette.showAnimated

    see the documentation on github for more info

    0 讨论(0)
  • 2020-12-23 18:56

    For future users people could user my plugin for Transition Support in marionette.

    https://github.com/saqibshakil/Marionette.TransitionRegion/

    I used css3 transitions as those have more hardware support than jquery animations. on the downside using this makes the code async so be carefull of that.

    0 讨论(0)
提交回复
热议问题