Skip to content

Instantly share code, notes, and snippets.

@j-greig
Created June 29, 2018 13:12
Show Gist options
  • Save j-greig/4ebdca41f3120f99dcfc84373694f2fc to your computer and use it in GitHub Desktop.
Save j-greig/4ebdca41f3120f99dcfc84373694f2fc to your computer and use it in GitHub Desktop.
Barba.js demo with special transition on one page only (About)
// Handler for when DOM is loaded....
var startup = function(){
console.log('DOM ready, starting Barba.js')
Barba.Pjax.start();
};
// Check if DOM is loaded...
if (
document.readyState === "complete" ||
(document.readyState !== "loading" && !document.documentElement.doScroll)
) {
callback();
} else {
document.addEventListener("DOMContentLoaded", startup);
}
var FadeTransition = Barba.BaseTransition.extend({
start: function() {
/**
* This function is automatically called as soon the Transition starts
*/
// 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();
});
}
});
var FadeUpTransition = 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
*/
// 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.fadeUpIn.bind(this));
},
fadeOut: function() {
/**
* this.oldContainer is the HTMLElement of the old Container
*/
return $(this.oldContainer).animate({ opacity: 0 }).promise();
},
fadeUpIn: 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)
*/
var _this = this;
var $el = $(this.newContainer);
$(this.oldContainer).hide();
$el.css({
marginTop : '+30px',
visibility : 'visible',
// opacity : 0
});
// $el.fadeIn();
$el.fadeIn().animate( {
'marginTop' : "-=30px"
}, 500, function() {
console.log('Sliding up...')
_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...
*/
console.log(window.location.pathname);
/**
* If entering the About page, use a special transition
*/
if (window.location.pathname == '/about') {
return FadeTransition;
/**
* Otherwise do the usual transition
*/
} else {
return FadeUpTransition;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment