Barba: New website for the "Examples" page

Created on 30 Oct 2017  路  5Comments  路  Source: barbajs/barba

Hello there, my latest website is proudly powered by Barba.js, I would be happy to see it in the "Examples" section :)

https://www.giuseppespota.com/

Thanks a lot! I will make a donation for this library, you totally deserve it!!

Most helpful comment

@tiansial it's quite easy to achieve :)

First of all, you have to listen for the newPageReady event and get the newPageRawHTML parameter, just like this:

Barba.Dispatcher.on('newPageReady', function(currentStatus, prevStatus, HTMLElementContainer, newPageRawHTML) {
    const newDoc = parseHTML(newPageRawHTML);
});

That parameter, as the name suggests, is a string containing the whole new page HTML, you just need to parse it using a simple function that I called parseHTML().

function parseHTML(html) {
    var parser = new DOMParser();
    return parser.parseFromString(html, 'text/html');
}

This way you can obtain the new page's document object as value of the newDoc constant.

Finally, you have to get the HTML of the WPML language switcher (or anything else that's outside the Barba.js container) from the newPage document object and inject it into the actual page's language switcher.

Barba.Dispatcher.on('newPageReady', function(currentStatus, prevStatus, HTMLElementContainer, newPageRawHTML) {

    // Getting the new page's document object
    const newDoc = parseHTML(newPageRawHTML); 

    // In this case I have to update the whole #masthead div
    const newPageMasthead = newDoc.getElementById('masthead');

    // The inner HTML of the new page's #masthead is injected into the current #masthead
    document.getElementById('masthead').innerHTML = newPageMasthead.innerHTML;

});

I changed the whole #masthead div in order to update the menu too, but this process can be used to update everything outside the Barba.js container.

Let me know if it's not clear ;)

All 5 comments

How did you get the WPML language switcher update itself outside the barba-container?

@tiansial it's quite easy to achieve :)

First of all, you have to listen for the newPageReady event and get the newPageRawHTML parameter, just like this:

Barba.Dispatcher.on('newPageReady', function(currentStatus, prevStatus, HTMLElementContainer, newPageRawHTML) {
    const newDoc = parseHTML(newPageRawHTML);
});

That parameter, as the name suggests, is a string containing the whole new page HTML, you just need to parse it using a simple function that I called parseHTML().

function parseHTML(html) {
    var parser = new DOMParser();
    return parser.parseFromString(html, 'text/html');
}

This way you can obtain the new page's document object as value of the newDoc constant.

Finally, you have to get the HTML of the WPML language switcher (or anything else that's outside the Barba.js container) from the newPage document object and inject it into the actual page's language switcher.

Barba.Dispatcher.on('newPageReady', function(currentStatus, prevStatus, HTMLElementContainer, newPageRawHTML) {

    // Getting the new page's document object
    const newDoc = parseHTML(newPageRawHTML); 

    // In this case I have to update the whole #masthead div
    const newPageMasthead = newDoc.getElementById('masthead');

    // The inner HTML of the new page's #masthead is injected into the current #masthead
    document.getElementById('masthead').innerHTML = newPageMasthead.innerHTML;

});

I changed the whole #masthead div in order to update the menu too, but this process can be used to update everything outside the Barba.js container.

Let me know if it's not clear ;)

Works like a charm, thanks. :)

@kekkorider Hi Francesco, I wanted to ask you how you do to change the body class and the data-namespace between each page. I couldn't solve this using Wordpress, can you help me please?

Your site is amazing, one day I want to do something as great as your site.
Have a nice day man! Thanks in advance!

@kekkorider Hi Fransesco, I saw your transition works like awesome. I wanted to ask you on how to do motion blur between each page transition

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iamtompickering picture iamtompickering  路  3Comments

lasagner picture lasagner  路  3Comments

pburdylo picture pburdylo  路  3Comments

hugobqd picture hugobqd  路  3Comments

S1SYPHOS picture S1SYPHOS  路  3Comments