3

I know this question has been asked in various places but I have yet to find an answer that works for my situation.

I'm using smoothstate.js to fire a series of animations when the page loads, and (hopefully) reverse the animations when the page exits.

Works great on page load but no luck on the reversing. The scroll to top doesn't seem to be working either.

Please see this fiddle: https://jsfiddle.net/bridget_kilgallon/jxh2urgg/

JS:

;(function ($) {
  'use strict';
  var content  = $('#main').smoothState({
        // onStart runs as soon as link has been activated
        onStart : {

          // Set the duration of our animation
          duration: 250,

          // Alterations to the page
          render: function () {

            // Quickly toggles a class and restarts css animations
            content.toggleAnimationClass('is-exiting');
          }
        }
      }).data('smoothState'); // makes public methods available
})(jQuery);

;(function ($) {
  'use strict';
  var $body    = $('html, body'), // Define jQuery collection 
      content  = $('#main').smoothState({
        onStart : {
          duration: 250,
          render: function () {
            content.toggleAnimationClass('is-exiting');

            // Scroll user to the top
            $body.animate({ 'scrollTop': 0 });

          }
        }
      }).data('smoothState');
})(jQuery);

SCSS:

/** Reverse "exit" animations */
.m-scene.is-exiting {
    .scene-element {
      animation-direction: alternate-reverse;
      -webkit-animation-direction: alternate-reverse;
      -moz-animation-direction: alternate-reverse;
    }
}
Bridget Kilgallon
  • 325
  • 2
  • 3
  • 9

3 Answers3

2

toggleAnimationClass() has been deprecated. Use restartCSSAnimations() instead, and add or remove the animation classes on the appropiate callbacks.

For example:

var smoothState = $page.smoothState({
    onStart: {
      duration: 250,
      render: function (url, $container) {
        // Add your CSS animation reversing class
        $page.addClass('is-exiting');

        // Restart your animation
        smoothState.restartCSSAnimations();

        // anything else
      }
    },
    onEnd: {
      duration: 0,
      render: function (url, $container, $content) {
        // Remove your CSS animation reversing class
        $page.removeClass('is-exiting');

        // Inject the new content
        $container.html($content);
      }
    }
  }).data('smoothState');
  • Hi, this isn't work, my #main not getting the .is-exiting at all, any idea? I've been put jquery.min.js, put this function on all.js, having 2 pages that contains #main and .m-scene | I' did anything that I know I should do (as far as my knowledge) - but still not giving .is-exiting – Budi Tanrim Jun 28 '15 at 17:35
  • Shouldn't `$page` be `$container`? – Kristian Matthews Jul 04 '15 at 18:56
2

Ran into the same issue. It's important to read the code of the restartCSSAnimations function.

It only restarts animations tied to the main container object. In my case I had animations on children which did not fire.

My solution was to add a 'pt-reverse' class to any element that need a reverse animation. In the onStart I then added the following instead of restartCSSAnimations:

els = $('.pt-reverse');
$.each(els,function(ix,el) {
    var animation = $(el).css('animation-name');
    $(el).css('animation-name','none');
    $(el).height();
    $(el).css('animation-name',animation);
});

// smoothState.restartCSSAnimations();

This does essentially what restartCSSAnimations does, except more targeted to specific elements.

Now it's working smoothly.

theoretisch
  • 1,718
  • 5
  • 24
  • 34
Jan Klier
  • 21
  • 1
0

Check the duration of your animation and ensure that it's lasting the same amount of time as you've set for your duration key/value pair in JavaScript.

dapinitial
  • 123
  • 3
  • 10