cameronmcefee / plax

JQuery powered parallaxing
http://www.cameronmcefee.com/plax
MIT License
2.28k stars 206 forks source link

Plax + Turbolinks [It worcks] #74

Open Golgarud opened 6 years ago

Golgarud commented 6 years ago

Hello, First of all, thanks so much for this awesome plugin. (Im really sad about the support ending).

This is not realy an issue but I have try to run plax with Turbolinks. I cant find any explanation of how to do it. So I found it myself and I want to post the solution here :3

( function() {
    Turbolinks.start();
    var ready = function()
    {
        console.log( "🦄 TurboLinks Ready -> " + window.location.href + " width: " + $(window).width());
        // PLAX
        if( $('.yourPlaxSelector').length )
        {
            $('.yourPlaxSelector').plaxify();
            $.plax.enable();
        }
    }
    var cache = function()
    {
        console.log( "🦄 TurboLinks CacheLoad" );
        // Allow plax function `inViewport` to worck properly.
        // Without it Plax will only worck on the first call of `ready`.
        $.plax.disable( { "clearLayers": true } );
    }
    // Turbolink tracker
    var change = function()
    {
        console.log( "🦄 TurboLinks Change" );
        window['referer'] = window.location.href;
    }
    ready();
    document.addEventListener("turbolinks:before-cache", cache);
    document.addEventListener("turbolinks:load", ready);
    document.addEventListener("page:change", change);
} )( jQuery );