barbajs-Transition

transition 是一個Javascript對象,它將確保隱藏舊的容器并顯示新的容器。
所有的transitions都需要擴展自Barba.BaseTransiton對象。

請注意:即使在加載新頁面之前,轉換也會開始。這樣,即使在加載下一頁之前,您也可以開始自己的轉換。

成員 描述
start 當你的轉場動畫開始時會自動調用這個函數。 (你可以認為它是轉場函數的構造函數)
done 當您的轉場動畫完成時需要調用的函數。 不要忘了調用這個功能!
oldContainer 舊container的HTMLElement.
newContainerLoading Promise 表示在加載下一個容器。
newContainer 新容器的HTMLElement。 (狀態(tài)是visibility: hidden;) 請注意,在newContainerLoading狀態(tài)是resolved之前它是undefined!

HideShow 例子
默認情況下,barba.js使用一個簡單的HideShow轉場動畫。為了理解它是如何工作的,讓我們重新創(chuàng)建它:

var HideShowTransition = Barba.BaseTransition.extend({
  start: function() {
    this.newContainerLoading.then(this.finish.bind(this));
  },

  finish: function() {
    document.body.scrollTop = 0;
    this.done();
  }
});

接下來,我們必須告訴barba.js使用我們的新過渡:

Barba.Pjax.getTransition = function() {
  return HideShowTransition;
};

漸變例子
現在讓我們創(chuàng)建一個更復雜的轉換,一個使用jQuery的.animate()的FadeTransition

當然你可以用任何的js庫,js方法,css轉換,或任何的其他形式

var FadeTransition = Barba.BaseTransition.extend({
  start: function() {
    /**
     * This function is automatically called as soon the Transition starts
     * this.newContainerLoading is a Promise for the loading of the new container
     * (Barba.js also comes with an handy Promise polyfill!)
     */

    // As soon the loading is finished and the old page is faded out, let's fade the new page
    Promise
      .all([this.newContainerLoading, this.fadeOut()])
      .then(this.fadeIn.bind(this));
  },

  fadeOut: function() {
    /**
     * this.oldContainer is the HTMLElement of the old Container
     */

    return $(this.oldContainer).animate({ opacity: 0 }).promise();
  },

  fadeIn: function() {
    /**
     * this.newContainer is the HTMLElement of the new Container
     * At this stage newContainer is on the DOM (inside our #barba-container and with visibility: hidden)
     * Please note, newContainer is available just after newContainerLoading is resolved!
     */

    var _this = this;
    var $el = $(this.newContainer);

    $(this.oldContainer).hide();

    $el.css({
      visibility : 'visible',
      opacity : 0
    });

    $el.animate({ opacity: 1 }, 400, function() {
      /**
       * Do not forget to call .done() as soon your transition is finished!
       * .done() will automatically remove from the DOM the old Container
       */

      _this.done();
    });
  }
});

/**
 * Next step, you have to tell Barba to use the new Transition
 */

Barba.Pjax.getTransition = function() {
  /**
   * Here you can use your own logic!
   * For example you can use different Transition based on the current page or link...
   */

  return FadeTransition;
};
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容