SmoothStateJS: how to keep an element from refreshing on page change?

1.3k views Asked by At

I am using SmoothState.Js to load new pages on clicks. Since on every one of those pages the logo stays in the same place/size, I would like for the logo to not refresh and not fadeout/in on page changes.

The links in my menu don't refresh but the logo does. I have not seen anything in the docs or on Google addressing this.. How would I go about keeping an image in place at all times in between these ajax page changes?

Here is the SmoothState call:

$(function() {
    $('#main').smoothState();
});

$(function() {
    "use strict";
    var options = {
            prefetch: true,
            pageCacheSize: 4,
            onStart: {
                duration: 300, // Duration of our animation 
                render: function($container) {
                    // Add your CSS animation reversing class 

                    $container.addClass("is-exiting");


                    // Restart your animation 
                    smoothState.restartCSSAnimations();
                }
            },
            onReady: {
                duration: 0,
                render: function($container, $newContent) {
                    // Remove your CSS animation reversing class 
                    $container.removeClass("is-exiting");

                    // Inject the new content 
                    $container.html($newContent);


                }

            },

        },
        smoothState = $("#main").smoothState(options).data("smoothState");
});

The HTML:

 <header class="header">
        <div class="main-logo">
            <a class="svg" href="www.site.com">
                <object class="willItBlend" data="../svg/main-logo.svg" type="image/svg+xml"></object>
                <img class="blender" src="../svg/main-logo.png" />
            </a>
        </div>
        <nav class="main-nav-about">
            // links go here
        </nav>
</header>

Available event calls for SmoothState:

onBefore - Runs before a page load has been started
onStart - Runs once a page load has been activated
onProgress - Runs if the page request is still pending and the onStart animations have finished
onReady - Run once the requested content is ready to be injected into the page and the previous animations have finished
onAfter - Runs after the new content has been injected into the page and all animations are complete
2

There are 2 answers

0
RobM On

You need to use the 'blacklist' option as specified in the docs:

A jQuery selector specifying which elements within the smoothState element should be ignored. This includes both form and anchor elements.

Just add this to the options object blacklist: '.no-smoothState'

Then add the .no-smoothState class to any page elements you want to be ignored.

0
Jacek On

For me blacklist did not work in such situation. What I have done to keep menu not changing and replace only wanted content is presented below solution.

HTML (index.html, about.html and work.html are following the pattern):

<body>
    <div id="main">
        <div class="menu">
            <a href="index.html">index</a>
            <a href="work.html">work</a>
            <a href="about.html">about</a>
        </div>
        <div id="content">
            <h1>INDEX</h1>
        </div>
    </div>
</body>

JS:

$(function() {
  $('#main').smoothState({
    'onReady': {
        'render': function($container, $newContent) {

            // replace only content div and leave the menu alone
            $container.find('#content').html($newContent.filter('#content'));
        }
    }
  });
});

Please note, I haven't tested if everything else still works but it seems like all is good. I am new to the plugin itself and considering it for my next project. This solution came out as a result of some tests and experiments.